boqwij 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in boqwij.gemspec
4
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ boqwij (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ boqwij!
@@ -0,0 +1,117 @@
1
+ boQwIj
2
+ ==========
3
+ This is a series of methods and extensions I've written or collected from various sources over time to help me mainly in solving [Project Euler](http://projecteuler.net/) problems and other mathematical games. In case you are wondering about the name, 'boQwIj' is Klingon for 'my assistant', which is what these methods do for me when solving problems.
4
+
5
+ Installation
6
+ ------------
7
+ Installing this gem is pretty simple, although you have to remember that Ruby does not handle Klingon capitalization well. Anyways, just type on the command line:
8
+ $ gem install boqwij
9
+ And add this gem into any other code with:
10
+ require 'rubygems'
11
+ require 'boqwij'
12
+
13
+ Usage
14
+ ------------
15
+ There are basically four sections to this gem:
16
+
17
+ * Integer extensions
18
+ * Array extensions
19
+ * String extensions
20
+ * base module with special methods and constants
21
+
22
+
23
+
24
+ ### Integer Extensions ###
25
+
26
+ Added to the Integer class are the following methods. Most are self-explanatory and some have other Ruby equivalents already.
27
+ (I wrote code that I could remember easily to help me get through problems faster.)
28
+
29
+ **factorial** -- Get the factorial of given number.
30
+ (_e.g., 6.factorial # => 720_)
31
+
32
+ **fibonacci** -- Get the fibonacci number associated with a particular integer.
33
+ (_e.g., 6.fibonacci # => 8; 10.fibonacci # => 55_)
34
+
35
+ **is_fibonacci?** -- Checks to see if integer is contained in the fibonacci sequence.
36
+ (_e.g., 12.is_fibonacci? # => false; 55.is_fibonacci? # => true_)
37
+
38
+ **is_divisible_by?(factor)** -- Test if a number is divisible by a given factor.
39
+ (_e.g., 18.is_divisible_by?(3) #=> true_)
40
+
41
+ **is_prime?** -- Test to see if a given number is prime.
42
+ (_e.g., 43.is_prime? # => true; 4.is_prime? # => false_)
43
+
44
+ **prime_factors** -- Returns an array of prime factors for any integer.
45
+ (_e.g., 30.prime_factors # => [2,3,5]_)
46
+
47
+ **is_palindrome?** -- Test to see if a given number is a palindrome.
48
+ (_e.g., 1001.is_palindrome? # => true; 4654.is_palindrome? # => false_)
49
+
50
+ **is_permutation_of?(int)** -- Compares two integers to see if they are permutations of each other.
51
+ (_e.g., 314159.is_permutation_of?(431951) # => true_)
52
+
53
+ **length** -- Find the length of a particular integer.
54
+
55
+ **delimit** -- Add a delimiter to every three digits of a long integer. The default delimiter is a comma.
56
+
57
+ **even?** -- Test if a particular integer is even.
58
+
59
+ **odd?** -- Test if a particular integer is odd.
60
+
61
+ **neg?** -- Test if a particular integer is negative.
62
+
63
+ **pos?** -- Test if a particular integer is positive.
64
+
65
+
66
+
67
+ ### Array Extensions ###
68
+
69
+ Added to the Array class are the following methods:
70
+
71
+ **sum** -- Get the sum of an all-numeric array. Raises a runtime error if non-numeric array is used.
72
+
73
+ **product** -- Get the product of an all-numeric array. Raises a runtime error if non-numeric array is used.
74
+
75
+ **mean** -- Get the mean of an all-numeric array. Raises a runtime error if non-numeric array is used.
76
+
77
+ **median** -- Get the median of an all-numeric array. Raises a runtime error if non-numeric array is used.
78
+
79
+ **range** -- Get the range of values in an all-numeric array. Raises a runtime error if non-numeric array is used.
80
+
81
+ **stdev** -- Get the standard deviation of an all-numeric array. Raises a runtime error if non-numeric array is used.
82
+
83
+ **random** -- Get the first value of a randomized array.
84
+
85
+ **randomize** -- Randomize the order of an array.
86
+
87
+ **randomize!** -- Randomize the order of an array and make the change permanent.
88
+
89
+
90
+
91
+ ### String Extensions ###
92
+
93
+ Added to the String class are the following methods:
94
+
95
+ **capwords** -- A method to capitalize the first letter of each word in a string.
96
+
97
+ **is_palindrome?** -- A method to detect whether a given string is a palidrome. (_e.g., ‘civic’.is_palindrome? # => true_)
98
+
99
+ **parens** -- A method to wrap parenthese around a given string.
100
+
101
+
102
+
103
+ ### Base Module ###
104
+
105
+ The base module has the following methods and constants associated with it:
106
+
107
+ **PI_10000** -- Define 10,000 digits of pi. This is a string with no decimal place that can be parsed as needed.
108
+
109
+ **E_10000** -- Define 10,000 digits of e. This is a string with no decimal place that can be parsed as needed.
110
+
111
+ **get_prime_set(n, *starting_at)** -- Returns an array of prime numbers, the length of that array being n. Optional second argument to set a starting place because some problems want a set of 100 prime numbers greater than 1,000 (for example). The starting_at parameter will mean the first prime in the set has to be either this value or the next prime number greater than it. (_e.g., Boqwij.get_prime_set(5) # => [2, 3, 5, 7, 11]; Boqwij.get_prime_set(5,100) # => [101, 103, 107, 109, 113]_)
112
+
113
+ **get_fibonacci_set(n, *starting_at)** -- Returns an array of integers which is the Fibonacci sequence of length n. Optional second parameter is 'starting_at' because some problems want a set of 25 Fibonacci numbers, but starting at a particular value such as 100. (_e.g., Boqwij.get_fibonacci_set(6) # => [1, 1, 2, 3, 5, 8]_)
114
+
115
+ **is_pythagorean_triplet?(a, b, c)** -- Determines if the three values given are a pythagorean triplet. (_e.g., Boqwij.is_pythagorean_triplet?(3, 4, 5) # => true_)
116
+
117
+ **find_missing_pyth_triplet(a, b, c)** -- Given any two values of a pythagorean triplet, method returns the missing value. (_e.g., Boqwij.find_missing_pyth_triplet(3, 4, nil) # => 5_)
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "boqwij/version"
4
+
5
+
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "boqwij"
9
+ s.version = Boqwij::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["Klingon Code Warrior"]
12
+ s.email = ["profh@cmu.edu"]
13
+ s.homepage = "http://rubygems.org/gems/boqwij"
14
+ s.summary = %q{A series of helpers and extensions I use for solving Project Euler problems and other mathematical diversions.}
15
+ s.description = %q{This is a series of methods and extensions I've written or collected from various sources over time to help me mainly in solving Project Euler problems and other mathematical games. In case you are wondering about the name, boQwIj is Klingon for _my_assistant_, which is what these methods do for me when solving problems.}
16
+
17
+ s.rubyforge_project = "boqwij"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,4 @@
1
+ require 'boqwij/base'
2
+ require 'boqwij/integer_extensions'
3
+ require 'boqwij/array_extensions'
4
+ require 'boqwij/string_extensions'
@@ -0,0 +1,105 @@
1
+ module Boqwij
2
+ # A set of extensions to the Array class
3
+ module ArrayExtensions
4
+ # Get the sum of an all-numeric array.
5
+ # Raises a runtime error if non-numeric array is used.
6
+ def sum
7
+ begin
8
+ raise RuntimeError if self.empty?
9
+ self.inject do |sum, num|
10
+ raise RuntimeError unless num.kind_of?(Numeric)
11
+ sum + num
12
+ end
13
+ rescue Exception => e
14
+ "The sum could not be calculated for this array"
15
+ end
16
+ end
17
+
18
+ # Get the product of an all-numeric array.
19
+ # Raises a runtime error if non-numeric array is used.
20
+ def product
21
+ begin
22
+ raise RuntimeError if self.empty?
23
+ self.inject do |product, num|
24
+ raise RuntimeError unless num.kind_of?(Numeric)
25
+ product * num
26
+ end
27
+ rescue Exception => e
28
+ "The product could not be calculated for this array"
29
+ end
30
+ end
31
+
32
+ # Get the mean of an all-numeric array.
33
+ # Raises a runtime error if non-numeric array is used.
34
+ def mean
35
+ begin
36
+ raise RuntimeError if self.empty?
37
+ self.each do |value|
38
+ raise RuntimeError unless value.kind_of?(Numeric)
39
+ end
40
+ self.sum.to_f / self.length
41
+ rescue Exception => e
42
+ "The mean could not be calculated for this array"
43
+ end
44
+ end
45
+
46
+ # Get the median of an all-numeric array.
47
+ # Raises a runtime error if non-numeric array is used.
48
+ def median
49
+ begin
50
+ raise RuntimeError if self.empty?
51
+ (self.sort!.size.odd?) ? self[(self.size/2)] : (self[(self.size/2)] + self[((self.size-1)/2)])/2.0
52
+ rescue Exception => e
53
+ "The median could not be calculated for this array"
54
+ end
55
+ end
56
+
57
+ # Get the range of values in an all-numeric array.
58
+ # Raises a runtime error if non-numeric array is used.
59
+ def range
60
+ begin
61
+ raise RuntimeError if self.empty?
62
+ (self.max - self.min)
63
+ rescue Exception => e
64
+ "The range could not be calculated for this array"
65
+ end
66
+ end
67
+
68
+ # Get the standard deviation of an all-numeric array.
69
+ # Raises a runtime error if non-numeric array is used.
70
+ def stdev
71
+ begin
72
+ raise RuntimeError if self.empty?
73
+ # self.each {|i| raise RuntimeError if unless i.kind_of?(Numeric)}
74
+ mean = self.mean
75
+ n = self.length
76
+ if n.zero?
77
+ return nil
78
+ else
79
+ sq_dist = self.inject(0) { |sum, num| sum += (num - mean)**2 }
80
+ stdev = Math.sqrt(sq_dist/(n-1))
81
+ end
82
+ rescue Exception => e
83
+ "The stddev could not be calculated for this array"
84
+ end
85
+ end
86
+
87
+ # Randomize the order of an array.
88
+ def randomize
89
+ self.sort_by { rand }
90
+ end
91
+
92
+ # Randomize the order of an array and make the change permanent.
93
+ def randomize!
94
+ self.replace(self.randomize)
95
+ end
96
+
97
+ # Get the first value of a randomized array.
98
+ def random
99
+ self.randomize.first
100
+ end
101
+ end
102
+ end
103
+
104
+ # Add methods to the Array class
105
+ Array.send :include, Boqwij::ArrayExtensions
@@ -0,0 +1,60 @@
1
+ module Boqwij
2
+ extend self
3
+
4
+ # Define a couple of constants for use in various problems.
5
+ # Define 10,000 digits of e. This is a string with no decimal place that can be parsed as needed.
6
+ E_10000 = "2718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337998016125492278509257782562092622648326277933386566481627725164019105900491644998289315056604725802778631864155195653244258698294695930801915298721172556347546396447910145904090586298496791287406870504895858671747985466775757320568128845920541334053922000113786300945560688166740016984205580403363795376452030402432256613527836951177883863874439662532249850654995886234281899707733276171783928034946501434558897071942586398772754710962953741521115136835062752602326484728703920764310059584116612054529703023647254929666938115137322753645098889031360205724817658511806303644281231496550704751025446501172721155519486685080036853228183152196003735625279449515828418829478761085263981395599006737648292244375287184624578036192981971399147564488262603903381441823262515097482798777996437308997038886778227138360577297882412561190717663946507063304527954661855096666185664709711344474016070462621568071748187784437143698821855967095910259686200235371858874856965220005031173439207321139080329363447972735595527734907178379342163701205005451326383544000186323991490705479778056697853358048966906295119432473099587655236812859041383241160722602998330535370876138939639177957454016137223618789365260538155841587186925538606164779834025435128439612946035291332594279490433729908573158029095863138268329147711639633709240031689458636060645845925126994655724839186564209752685082307544254599376917041977780085362730941710163434907696423722294352366125572508814779223151974778060569672538017180776360346245927877846585065605078084421152969752189087401966090665180351650179250461950136658543663271254963990854914420001457476081930221206602433009641270489439039717719518069908699860663658323227870937650226014929101151717763594460202324930028040186772391028809786660565118326004368850881715723866984224220102495055188169480322100251542649463981287367765892768816359831247788652014117411091360116499507662907794364600585194199856016264790761532103872755712699251827568798930276176114616254935649590379804583818232336861201624373656984670378585330527583333793990752166069238053369887956513728559388349989470741618155012539706464817194670834819721448889879067650379590366967249499254527903372963616265897603949857674139735944102374432970935547798262961459144293645142861715858733974679189757121195618738578364475844842355558105002561149239151889309946342841393608038309166281881150371528496705974162562823609216807515017772538740256425347087908913729172282861151591568372524163077225440633787593105982676094420326192428531701878177296023541306067213604600038966109364709514141718577701418060644363681546444005331608778314317444081194942297559931401188868331483280270655383300469329011574414756313999722170380461709289457909627166226074071874997535921275608441473782330327033016823719364800217328573493594756433412994302485023573221459784328264142168487872167336701061509424345698440187331281010794512722373788612605816566805371439612788873252737389039289050686532413806279602593038772769778379286840932536588073398845721874602100531148335132385004782716937621800490479559795929059165547050577751430817511269898518840871856402603530558373783242292418562564425502267215598027401261797192804713960068916382866527700975276706977703643926022437284184088325184877047263844037953016690546593746161932384036389313136432713768884102681121989127522305625675625470172508634976536728860596675274086862740791285657699631378975303466061666980421826772456053066077389962421834085988207186468262321508028828635974683965435885668550377313129658797581050121491620765676995065971534476347032085321560367482860837865680307306265763346977429563464371670939719306087696349532884683361303882943104080029687386911706666614680001512114344225602387447432525076938707777519329994213727721125884360871583483562696166198057252661220679754062106208064988291845439530152998209250300549825704339055357016865312052649561485724925738620691740369521353373253166634546658859728665945113644137033139367211856955395210845840724432383558606310680696492485123263269951460359603729725319836842336390463213671011619282171115028280160448805880238203198149309636959673583274202498824568494127386056649135252670604623445054922758115170931492187959271800194096886698683703730220047531433818109270803001720593553052070070607223399946399057131158709963577735902719628506114651483752620956534671329002599439766311454590268589897911583709341937044115512192011716488056694593813118384376562062784631049034629395002945834116482411496975832601180073169943739350696629571241027323913874175492307186245454322203955273529524024590380574450289224688628533654221381572213116328811205214648980518009202471939171055539011394331668151582884368760696110250517100739276238555338627255353883096067164466237092264680967125406186950214317621166814009759528149390722260111268115310838731761732323526360583817315103459573653822353499293582283685100781088463434998351840445170427018938199424341009057537625776757111809008816418331920196262341628816652137471732547772"
7
+ # Define 10,000 digits of pi. This is a string with no decimal place that can be parsed as needed.
8
+ PI_10000 = "3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104752162056966024058038150193511253382430035587640247496473263914199272604269922796782354781636009341721641219924586315030286182974555706749838505494588586926995690927210797509302955321165344987202755960236480665499119881834797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548161361157352552133475741849468438523323907394143334547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383827967976681454100953883786360950680064225125205117392984896084128488626945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645995813390478027590099465764078951269468398352595709825822620522489407726719478268482601476990902640136394437455305068203496252451749399651431429809190659250937221696461515709858387410597885959772975498930161753928468138268683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244136549762780797715691435997700129616089441694868555848406353422072225828488648158456028506016842739452267467678895252138522549954666727823986456596116354886230577456498035593634568174324112515076069479451096596094025228879710893145669136867228748940560101503308617928680920874760917824938589009714909675985261365549781893129784821682998948722658804857564014270477555132379641451523746234364542858444795265867821051141354735739523113427166102135969536231442952484937187110145765403590279934403742007310578539062198387447808478489683321445713868751943506430218453191048481005370614680674919278191197939952061419663428754440643745123718192179998391015919561814675142691239748940907186494231961567945208095146550225231603881930142093762137855956638937787083039069792077346722182562599661501421503068038447734549202605414665925201497442850732518666002132434088190710486331734649651453905796268561005508106658796998163574736384052571459102897064140110971206280439039759515677157700420337869936007230558763176359421873125147120532928191826186125867321579198414848829164470609575270695722091756711672291098169091528017350671274858322287183520935396572512108357915136988209144421006751033467110314126711136990865851639831501970165151168517143765761835155650884909989859982387345528331635507647918535893226185489632132933089857064204675259070915481416549859461637180270981994309924488957571282890592323326097299712084433573265489382391193259746366730583604142813883032038249037589852437441702913276561809377344403070746921120191302033038019762110110044929321516084244485963766983895228684783123552658213144957685726243344189303968642624341077322697802807318915441101044682325271620105265227211166039666557309254711055785376346682065310989652691862056476931257058635662018558100729360659876486117910453348850346113657686753249441668039626579787718556084552965412665408530614344431858676975145661406800700237877659134401712749470420562230538994561314071127000407854733269939081454664645880797270826683063432858785698305235808933065757406795457163775254202114955761581400250126228594130216471550979259230990796547376125517656751357517829666454779174501129961489030463994713296210734043751895735961458901938971311179042978285647503203198691514028708085990480109412147221317947647772622414254854540332157185306142288137585043063321751829798662237172159160771669254748738986654949450114654062843366393790039769265672146385306736096571209180763832716641627488880078692560290228472104031721186082041900042296617119637792133757511495950156604963186294726547364252308177036751590673502350728354056704038674351362222477158915049530984448933309634087807693259939780541934144737744184263129860809988868741326047215695162396586457302163159819319516735381297416772947867242292465436680098067692823828068996400482435403701416314965897940924323789690706977942236250822168895738379862300159377647165122893578601588161755782973523344604281512627203734314653197777416031990665541876397929334419521541341899485444734567383162499341913181480927777103863877343177207545654532207770921201905166096280490926360197598828161332316663652861932668633606273567630354477628035045077723554710585954870279081435624014517180624643626794561275318134078330336254232783944975382437205835311477119926063813346776879695970309833913077109870408591337464144282277263465947047458784778720192771528073176790770715721344473060570073349243693113835049316312840425121925651798069411352801314701304781643788518529092854520116583934196562134914341595625865865570552690496520985803385072242648293972858478316305777756068887644624824685792603953527734803048029005876075825104747091643961362676044925627420420832085661190625454337213153595845068772460290161876679524061634252257719542916299193064553779914037340432875262888963995879475729174642635745525407909145135711136941091193932519107602082520261879853188770584297259167781314969900901921169717372784768472686084900337702424291651300500516832336435038951702989392233451722013812806965011784408745196012122859937162313017114448464090389064495444006198690754851602632750529834918740786680881833851022833450850486082503930213321971551843063545500766828294930413776552793975175461395398468339363830474611996653858153842056853386218672523340283087112328278921250771262946322956398989893582116745627010218356462201349671518819097303811980049734072396103685406643193950979019069963955245300545058068550195673022921913933918568034490398205955100226353536192041994745538593810234395544959778377902374216172711172364343543947822181852862408514006660443325888569867054315470696574745855033232334210730154594051655379068662733379958511562578432298827372319898757141595781119635833005940873068121602876496286744604774649159950549737425626901049037781986835938146574126804925648798556145372347867330390468838343634655379498641927056387293174872332083760112302991136793862708943879936201629515413371424892830722012690147546684765357616477379467520049075715552781965362132392640616013635815590742202020318727760527721900556148425551879253034351398442532234157623361064250639049750086562710953591946589751413103482276930624743536325691607815478181152843667957061108615331504452127473924544945423682886061340841486377670096120715124914043027253860764823634143346235189757664521641376796903149501910857598442391986291642193994907236234646844117394032659184044378051333894525742399508296591228508555821572503107125701266830240292952522011872676756220415420516184163484756516999811614101002996078386909291603028840026910414079288621507842451670908700069928212066041837180653556725253256753286129104248776182582976515795984703562226293486003415872298053498965022629174878820273420922224533985626476691490556284250391275771028402799806636582548892648802545661017296702664076559042909945681506526530537182941270336931378517860904070866711496558343434769338578171138645587367812301458768712660348913909562009939361031029161615288138437909904231747336394804575931493140529763475748119356709110137751721008031559024853090669203767192203322909433467685142214477379393751703443661991040337511173547191855046449026365512816228824462575916333039107225383742182140883508657391771509682887478265699599574490661758344137522397096834080053559849175417381883999446974867626551658276584835884531427756879002909517028352971634456212964043523117600665101241200659755851276178583829204197484423608007193045761893234922927965019875187212726750798125547095890455635792122103334669749923563025494780249011419521238281530911407907386025152274299581807247162591668545133312394804947079119153267343028244186041426363954800044800267049624820179289647669758318327131425170296923488962766844032326092752496035799646925650493681836090032380929345958897069536534940603402166544375589004563288225054525564056448246515187547119621844396582533754388569094113031509526179378002974120766514793942590298969594699556576121865619673378623625612521632086286922210327488921865436480229678070576561514463204692790682120738837781423356282360896320806822246801224826117718589638140918390367367222088832151375560037279839400415297002878307667094447456013455641725437090697939612257142989467154357846878861444581231459357198492252847160504922124247014121478057345510500801908699603302763478708108175450119307141223390866393833952942578690507643100638351983438934159613185434754649556978103829309716465143840700707360411237359984345225161050702705623526601276484830840761183013052793205427462865403603674532865105706587488225698157936789766974220575059683440869735020141020672358502007245225632651341055924019027421624843914035998953539459094407046912091409387001264560016237428802109276457931065792295524988727584610126483699989225695968815920560010165525637568"
9
+
10
+ # Returns an array of prime numbers, the length of that array being n.
11
+ # Optional second argument to set a starting place because some problems want a
12
+ # set of 100 prime numbers greater than 1,000 (for example). The starting_at
13
+ # parameter will mean the first prime in the set has to be greater than this
14
+ # given value. (e.g., Boqwij.get_prime_set(5) # => [2, 3, 5, 7, 11])
15
+ def get_prime_set(n, *starting_at)
16
+ @prime_set = Array.new
17
+ st_point = starting_at[0] || 2
18
+ count, current = 0, st_point
19
+ while count < n
20
+ if current.is_prime?
21
+ @prime_set << current
22
+ count += 1
23
+ end
24
+ current += 1
25
+ end
26
+ @prime_set
27
+ end
28
+
29
+ # Returns an array of integers which is the Fibonacci sequence of length n.
30
+ # Optional second parameter is 'starting_at' because some problems want a set of 25 Fibonacci
31
+ # numbers, but starting at a particular value such as 100.
32
+ def get_fibonacci_set(n, *starting_at)
33
+ @fibonacci_set = Array.new
34
+ st_point = starting_at[0] || 1
35
+ st_point.upto(n+st_point-1) do |i|
36
+ @fibonacci_set << i.fibonacci
37
+ end
38
+ @fibonacci_set
39
+ end
40
+
41
+ # Determines if the three values given are a pythagorean triplet.
42
+ # (e.g., Boqwij.is_pythagorean_triplet?(3, 4, 5) # => true)
43
+ def is_pythagorean_triplet?(a, b, c)
44
+ a**2 + b**2 == c**2
45
+ end
46
+
47
+ # Given any two values of a pythagorean triplet, method returns the missing value.
48
+ # (e.g., Boqwij.find_missing_pyth_triplet(3, 4, nil) # => 5)
49
+ def find_missing_pyth_triplet(a, b, c)
50
+ if a.nil?
51
+ Math.sqrt(c**2 - b**2)
52
+ elsif b.nil?
53
+ Math.sqrt(c**2 - a**2)
54
+ elsif c.nil?
55
+ Math.sqrt(a**2 + b**2)
56
+ else
57
+ nil
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,148 @@
1
+ module Boqwij
2
+ # A set of extensions to the Integer class.
3
+ module IntegerExtensions
4
+ # Test if integer is odd.
5
+ def odd?
6
+ self%2 != 0
7
+ end
8
+
9
+ # Test if integer is even.
10
+ def even?
11
+ !self.odd?
12
+ end
13
+
14
+ # Test if integer is negative.
15
+ def neg?
16
+ self < 0
17
+ end
18
+
19
+ # Test if integer is positive.
20
+ def pos?
21
+ self > 0
22
+ end
23
+
24
+ # Find the length of an integer.
25
+ def length
26
+ n, count = self, 1
27
+ while n > 9
28
+ n /= 10
29
+ count += 1
30
+ end
31
+ count
32
+ end
33
+
34
+ # Add a delimiter to every three digits of a long integer. The default delimiter is a comma.
35
+ def delimit(delimiter = ",")
36
+ self.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
37
+ rescue
38
+ self.to_s
39
+ end
40
+
41
+ # Get the factorial of given number.
42
+ # (e.g., 6.factorial # => 720)
43
+ def factorial
44
+ return nil if self < 1
45
+ n = self
46
+ count = n
47
+ fact = 1
48
+ count.times do
49
+ fact = fact * n
50
+ n -= 1
51
+ end
52
+ fact
53
+ end
54
+
55
+ # Get the fibonacci number associated with a particular integer.
56
+ # (e.g., 6.fibonacci # => 8; 10.fibonacci # => 55)
57
+ def fibonacci
58
+ return nil if self < 1
59
+ current, successor = 0, 1
60
+ self.times { |i| current, successor = successor, current + successor}
61
+ current
62
+ end
63
+
64
+ # Checks to see if integer is contained in the fibonacci sequence.
65
+ # (e.g, 12.is_fibonacci? # => false; 55.is_fibonacci? # => true)
66
+ def is_fibonacci?
67
+ return true if self == 1
68
+ a, b = Math.sqrt((5*(self**2))+4), Math.sqrt((5*(self**2))-4)
69
+ return true if (a.to_i == a and b.to_i != b) or (a.to_i != a && b.to_i == b)
70
+ return false
71
+ end
72
+
73
+ # Test to see if a given number is prime.
74
+ # (e.g., 2.is_prime? # => true; 104743.is_prime? # => true;
75
+ # e.g., 4.is_prime? # => false; 104742.is_prime? # => false)
76
+ def is_prime?
77
+ if self == 1 then return false
78
+ elsif self < 4 then return true
79
+ elsif self % 2 == 0 then return false
80
+ elsif self < 9 then return true
81
+ elsif self % 3 == 0 then return false
82
+ else
83
+ 5.step(Math.sqrt(self).to_i, 2) do |x|
84
+ return false if self % x == 0
85
+ end
86
+ end
87
+ return true
88
+ end
89
+
90
+ # Get an array of prime factors for any integer.
91
+ # (e.g., 30.prime_factors # => [2,3,5])
92
+ def prime_factors
93
+ return [self] if self.is_prime?
94
+ current, to_factor, factors, max = 2, self, [], 0
95
+ while to_factor % current == 0
96
+ factors << current
97
+ to_factor /= current
98
+ end
99
+ current += 1
100
+ max = Math.sqrt(to_factor).to_i + 1
101
+ while to_factor >= max
102
+ if to_factor % current == 0
103
+ factors << current
104
+ to_factor /= current
105
+ else
106
+ current += 2
107
+ end
108
+ end
109
+ factors << to_factor if to_factor > 1
110
+ factors
111
+ end
112
+
113
+ # Test if a number is divisible by a given factor.
114
+ # (e.g., 18.is_divisible_by?(3) #=> true)
115
+ def is_divisible_by?(factor)
116
+ self%factor == 0
117
+ end
118
+
119
+ # Test to see if a given number is a palindrome.
120
+ # (e.g., 1001.is_palindrome? # => true; 4654.is_palindrome? # => false)
121
+ def is_palindrome?
122
+ return false if self < 0
123
+ @original = self.to_s
124
+ @backwards = @original.reverse
125
+ @original.to_i == @backwards.to_i
126
+ end
127
+
128
+ # Compares two integers to see if they are permutations of each other.
129
+ # (e.g, 314159.is_permutation_of?(431951) # => true)
130
+ def is_permutation_of?(x)
131
+ return true if self == x
132
+ primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
133
+ s, s_total, x_total = self, 1, 1
134
+ while s > 0
135
+ s_total *= primes[s % 10]
136
+ s /= 10
137
+ end
138
+ while x > 0
139
+ x_total *= primes[x % 10]
140
+ x /= 10
141
+ end
142
+ s_total == x_total
143
+ end
144
+ end
145
+ end
146
+
147
+ # Add methods to the Integer class
148
+ Integer.send :include, Boqwij::IntegerExtensions
@@ -0,0 +1,31 @@
1
+ module Boqwij
2
+ # A set of extensions to the String class.
3
+ module StringExtensions
4
+ # A method to capitalize the first letter of each word in a string.
5
+ def capwords
6
+ @words = self.split
7
+ @revised = %w[]
8
+ @words.each do |word|
9
+ @revised << word.capitalize
10
+ end
11
+ @final = @revised.join(" ")
12
+ end
13
+
14
+ # A method to wrap parenthese around a given string.
15
+ def parens
16
+ '(' + self.to_s() + ')'
17
+ end
18
+
19
+ # A method to detect whether a given string is a palidrome.
20
+ # (e.g., 'civic'.is_palindrome? # => true)
21
+ def is_palindrome?
22
+ @original = self
23
+ @backwards = @original.reverse
24
+ return false if @original.casecmp(@backwards) != 0
25
+ true
26
+ end
27
+ end
28
+ end
29
+
30
+ # Add methods to the String class
31
+ String.send :include, Boqwij::StringExtensions
@@ -0,0 +1,3 @@
1
+ module Boqwij
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boqwij
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Klingon Code Warrior
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-05 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: This is a series of methods and extensions I've written or collected from various sources over time to help me mainly in solving Project Euler problems and other mathematical games. In case you are wondering about the name, boQwIj is Klingon for _my_assistant_, which is what these methods do for me when solving problems.
22
+ email:
23
+ - profh@cmu.edu
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - README.mkd
35
+ - Rakefile
36
+ - boqwij.gemspec
37
+ - lib/boqwij.rb
38
+ - lib/boqwij/array_extensions.rb
39
+ - lib/boqwij/base.rb
40
+ - lib/boqwij/integer_extensions.rb
41
+ - lib/boqwij/string_extensions.rb
42
+ - lib/boqwij/version.rb
43
+ has_rdoc: true
44
+ homepage: http://rubygems.org/gems/boqwij
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: boqwij
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A series of helpers and extensions I use for solving Project Euler problems and other mathematical diversions.
75
+ test_files: []
76
+