r_math_plus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a7bdad3ee832e67e60f81b058983001b616a6c9
4
+ data.tar.gz: 4c7b07dc511d2281bd3abe4109b86b570533d4e1
5
+ SHA512:
6
+ metadata.gz: 91ee88368dd92c311b6beec7ebc9036dc76b711b229f8296c2c6d1b9ede210cf5d55767566787b49a5a550c0a1020437d381f95bac6cd851bac2b02d9c01ef21
7
+ data.tar.gz: 16150e3d61bdaa74a17cab523972d4bb67e29aa6dc0f0507afb8d75088e7dc78e566929a005b827089225a3c4b435864eb5d4b5305db68db7edd10b911bebaab
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rmathplus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 otamm
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Rmathplus
2
+
3
+ A gem designed due to my personal online puzzle solving habits; some of them may be a part
4
+ of Ruby's kernel already, the purpose of the gem is to unite some methods in a single place with easy
5
+ access to them. RMathPlus is a collection of methods that appear kinda often in puzzles, and also some
6
+ that do not show up that often but can clearly be reused. Designed with computational efficiency in mind,
7
+ some of the methods are interdependent and therefore it is a good idea to always load the whole gem instead
8
+ of requiring specific methods.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'r_math_plus'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rmathplus
25
+
26
+ ## Usage
27
+ The '.is_prime?' method is the base of all of the other methods and also available by itself.
28
+ Simple pass a number as a parameter and it will return a boolean value:
29
+ ```ruby
30
+ RMathPlus.is_prime?(3) # => true
31
+ RMathPlus.is_prime?(4) # => false
32
+ ```
33
+ The method also accepts an optional second parameter, an array containing prime numbers;
34
+ usage of the second parameter works best in an incrementing fashion (see this repo's lib/r_math_plus.rb '#prime_array' method for an example.)
35
+ Ex:
36
+ ```ruby
37
+ RMathPlus.is_prime?(5,[2,3]) # => true, faster execution
38
+ RMathPlus.is_prime?(8,[2,3,5,7]) # => false, faster execution
39
+ ```
40
+
41
+ The '.prime_array' method takes an integer as a parameter and returns an array with the primes lesser than or equal to the number:
42
+ ```ruby
43
+ RMathPlus.prime_array(10) # => [2,3,5,7]
44
+ ```
45
+
46
+ '.prime_factors' returns an array of the prime factors of a given number n. If n is a prime, it will return n.
47
+ Ex:
48
+ ```ruby
49
+ RMathPlus.prime_factors(10) # => [2,5]
50
+ RMathPlus.prime_factors(8) # => [2,2,2]
51
+ ```
52
+
53
+ Second optional parameter used in order to improve speed if the method will run through an array of
54
+ integers; its input should be the array returned by the '.prime_array' method with the array of numbers' biggest int as parameter.
55
+ Ex:
56
+ ```ruby
57
+ biggest_number_prime_array = RMathPlus.prime_array(10)
58
+ prime_factors = []
59
+ [2,4,6,7,9,10].each do |n|
60
+ prime_factors.push(RMathPlus.prime_factors(n,biggest_number_prime_array)) # will execute faster iterating through a single array, otherwise it would make a prime array for each of the numbers
61
+ end
62
+ # => prime_factors == [[2],[2,2],[2,3],[7],[3,3],[2,5]]
63
+ ```
64
+
65
+ '.lcm' is used to calculate the least common multiple of a given array of numbers. Dependent on the method above, the code on lib/r_math_plus.rb which defines
66
+ this method illustrates well an example of the use of the optional second parameter to the '.prime_factors' method above.
67
+ ```ruby
68
+ RMathPlus.lcm([2,4,8]) # => 8
69
+ RMathPlus.lcm([3,6,8]) # => 24
70
+ ```
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/[my-github-username]/rmathplus/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ Dir.glob('spec/tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,3 @@
1
+ module RMathPlus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,94 @@
1
+ require "r_math_plus/version"
2
+
3
+ module RMathPlus
4
+
5
+ def self.is_prime?(n,prime_array=false) # number to be checked as first parameter, optional second parameter to be used with the 'prime_array' method.
6
+ if n < 2
7
+ return false
8
+ elsif n == 2
9
+ return true
10
+ else
11
+ if prime_array
12
+ for i in (2..(n/2)+1) #it makes no sense to divide by numbers which would result in a result less than 2 (the smallest prime); the '+1' in the end is due to the default 'round down' in integer division in Ruby.
13
+ prime_array.each { |prime| return false if (n % prime) == 0 }
14
+ end
15
+ else
16
+ for i in (2..(n/2)+1)
17
+ if (n % i) == 0
18
+ return false
19
+ end
20
+ end
21
+ end
22
+ end
23
+ return true
24
+ end
25
+
26
+ def self.prime_array(n) #returns an array with all primes less than number n.
27
+ if n < 2
28
+ return []
29
+ else
30
+ prime_array = []
31
+ for i in (2..n)
32
+ prime_array.push(i) if RMathPlus.is_prime?(i,prime_array) # the own prime_array built so far is passed as an argument in order to improve processing speed.
33
+ end
34
+ end
35
+ return prime_array
36
+ end
37
+
38
+ def self.prime_factors(number, biggest_number_prime_array=false) #returns the prime factors of a given number. if n is prime, returns n. second parameter to improve speed if method would be utilized in a range of numbers.
39
+ factors = []
40
+ if biggest_number_prime_array
41
+ primes = biggest_number_prime_array
42
+ else
43
+ primes = RMathPlus.prime_array(number)
44
+ end
45
+
46
+ while true
47
+ signal = factors.size #signalizes when to break the loop.
48
+ primes.each do |prime|
49
+ if (number % prime) == 0
50
+ factors.push(prime)
51
+ number = number / prime
52
+ break
53
+ end
54
+ end
55
+ if signal == factors.size
56
+ factors.push(number)
57
+ break
58
+ end
59
+ end
60
+ factors = factors.sort
61
+ return factors - [1] #...because 1 is not a prime.
62
+ end
63
+
64
+ def self.lcm(nums) #method that returns the least common multiple between a given array of numbers.
65
+ nums = nums.uniq.sort
66
+ primes = RMathPlus.prime_array(nums[-1]) #biggest number in the whole array.
67
+ factorized_nums = [] #holds the prime factors for each value in nums array.
68
+ nums.each { |n| factorized_nums.push(RMathPlus.prime_factors(n,primes)) }
69
+
70
+ unique_lcm_factors = [] #individual prime factors of lcm
71
+ lcm_factors = {} #prime factors of lcm
72
+ for factors in factorized_nums
73
+ for factor in factors.uniq
74
+ if lcm_factors.include?(factor)
75
+ lcm_factors[factor] = factors.count(factor) if factors.count(factor) > lcm_factors[factor]
76
+ else
77
+ lcm_factors[factor] = factors.count(factor)
78
+ end
79
+ end
80
+ end
81
+
82
+ lcm = 1
83
+ lcm_factors.each { |factor,max_quantity| lcm *= factor ** max_quantity }
84
+ return lcm
85
+ end
86
+
87
+ end
88
+
89
+ odds = []
90
+ for i in (1..99)
91
+ odds.push(i) if (i % 2) == 1
92
+ end
93
+
94
+ puts RMathPlus.lcm(odds)
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'r_math_plus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "r_math_plus"
8
+ spec.version = RMathPlus::VERSION
9
+ spec.authors = ["Otávio Monteagudo"]
10
+ spec.email = ["oivatom@gmail.com"]
11
+ spec.summary = %q{ Math operations to use in puzzles. }
12
+ spec.description = %q{ Some extra math ops to the standard library which include prime number operations and the least common multiple of an array of numbers. }
13
+ spec.homepage = "https://github.com/otamm/RMathPlus"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0"
24
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe RMathPlus do
5
+
6
+ describe ".is_prime?" do
7
+
8
+ it "returns true when a number is prime" do
9
+ (RMathPlus.is_prime?(3)).should == true
10
+ end
11
+
12
+ it "returns false when a number is not prime" do
13
+ (RMathPlus.is_prime?(4)).should == false
14
+ end
15
+
16
+ it "returns true when a big prime number is passed as param" do
17
+ (RMathPlus.is_prime?(104729)).should == true
18
+ end
19
+
20
+ it "executes faster when an array is passed as second parameter" do
21
+ begin1 = Time.now
22
+ (RMathPlus.is_prime?(50,[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]))
23
+ finish1 = Time.now
24
+
25
+ begin2 = Time.now
26
+ (RMathPlus.is_prime?(50))
27
+ finish2 = Time.now
28
+ ((finish1 - begin1) > (finish2 - begin2)).should == true
29
+ end
30
+
31
+ end
32
+
33
+ describe ".prime_array" do
34
+
35
+ it "returns an array with all the primes lesser than or equal to its parameter" do
36
+ expect(RMathPlus.prime_array(7)).to eq [2,3,5,7]
37
+ end
38
+
39
+ it "returns an empty array if input < 2" do
40
+ expect(RMathPlus.prime_array(0)).to eq []
41
+ end
42
+
43
+ end
44
+
45
+ describe ".prime_factors" do
46
+
47
+ it "returns repeated prime factors of a given number" do
48
+ expect(RMathPlus.prime_factors(8)).to eq [2,2,2]
49
+ end
50
+
51
+ it "returns its input if the input is a prime" do
52
+ expect(RMathPlus.prime_factors(11)).to eq [11]
53
+ end
54
+
55
+ end
56
+
57
+ describe ".lcm" do
58
+
59
+ it "returns the least common multiple of an array of numbers" do
60
+ expect(RMathPlus.lcm([2,4,8])).to eq 8
61
+ end
62
+
63
+ it "returns the least common multiple of a big array of numbers" do
64
+ odds = []
65
+ for i in (1..99)
66
+ odds.push(i) if (i % 2) == 1
67
+ end
68
+
69
+ expect(RMathPlus.lcm(odds)).to eq 1089380862964257455695840764614254743075
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1 @@
1
+ require 'r_math_plus'
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r_math_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Otávio Monteagudo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description: " Some extra math ops to the standard library which include prime number
56
+ operations and the least common multiple of an array of numbers. "
57
+ email:
58
+ - oivatom@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/r_math_plus.rb
69
+ - lib/r_math_plus/version.rb
70
+ - r_math_plus.gemspec
71
+ - spec/r_math_plus_spec.rb
72
+ - spec/spec_helper.rb
73
+ - spec/tasks/rspec.rake
74
+ homepage: https://github.com/otamm/RMathPlus
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Math operations to use in puzzles.
98
+ test_files:
99
+ - spec/r_math_plus_spec.rb
100
+ - spec/spec_helper.rb
101
+ - spec/tasks/rspec.rake