Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions problem03.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'prime' #allows the program to use the prime class


testNum = 600851475143 # number to get the primes of
$primeFactors = [] # array for storing prime factors

def getPrimeFactors(n)
i = 2 # i will be the divisor, which will start at 2 since all numbers are divisible by 1
while (i<=n) do
# if i is evenly divisible into the test number, see if it is a prime, and if so, push it into the prime array
if n%i == 0
if Prime.prime?(i)
$primeFactors.push(i)
n = n/i # make the test number the quotient of the found prime factor
end
end
i+=1
end
end

getPrimeFactors(testNum)
puts "The prime factors of #{testNum} are #{$primeFactors}.\n The largest prime factor is #{$primeFactors.max}."
37 changes: 37 additions & 0 deletions problem05.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# takes an input of a min and max value to establish a range, as well as a start number
def getTestArray(min, max, startnumber)
$testArray = Array(min..max)
$number = startnumber
end

def testModulo(n, factor)
# n = number to start from
# factor = number to increase by
result=true

$testArray.each { |i|
puts "#{n}/#{i}"
if n%i != 0
result = false
puts "#{n}/#{i} has a remainder - no good!"
break
end
}

if result == true
puts "Success!"
return true
else
$number += factor
puts "New number: #{$number}"
return false
end
end

getTestArray(1,20,232790000) #inputs 1-20 for the range and a large start number so the program does not take long to find the right number

while testModulo($number, $testArray.max) == false do
testModulo($number, $testArray.max)
end

puts "The smallest number that is a multiple of #{$testArray.min} thru #{$testArray.max} is #{$number}"
70 changes: 70 additions & 0 deletions problem08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
a = %w{73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450}

b="" #empty string

#concatenates each line of 'a' to make one long string, b
a.each do |i|
b = b + i
end

num_digits = 5 #number of adjacent digits to scan
products = [] #empty array to store the products
position = 0 #variable to indicate at what position the scan is at during each iteration, starts at 0

while position <= (b.length - num_digits)
has_zero = false #boolean to store whether a 0 has been found
digits = [] #array to store the digits
count = position #variable to move up through the digits of each iteration
product = 1 #variable to store the product of each iteration, starts at 1 for each iteration

puts "- POSTION #{position} of #{b.length}"

for i in 0..(num_digits - 1)
digits[i] = b[count..count].to_i
if digits[i] == 0
has_zero = true
break
else
count += 1
end
end

if has_zero
product = 0
else
digits.each do |i|
product *= i
end
end
puts "product: #{product}"
products.push(product)
position += 1
end


if products.max == nil || products.max == products.min
puts "There is no maximum product in this number."
else
puts "The greatest product of #{num_digits} adjacent digits is #{products.max} at position #{products.rindex(products.max)} in the #{b.length} digit number."
end