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
21 changes: 21 additions & 0 deletions problem03.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Goal: Answer the question "What is the largest prime factor of the number 600851475143?"
# Project Euler - Problem 3
# Daniel Stair


require 'prime'

# start at half the number given (round down if decimal). Next smallest prime factor can't exceed this amount
testnum = 600851475143
max_factor = Math.sqrt(testnum)
prime_factor_array = []

for i in 1..max_factor
if testnum.to_f % i.to_f == 0 && i.prime?
# add it to a list of all prime factors
prime_factor_array << i
end
end

puts "Valid prime factors of "+testnum.to_s+" are "+prime_factor_array.to_s
puts "the largest prime factor of "+testnum.to_s+" is "+prime_factor_array.max.to_s
24 changes: 24 additions & 0 deletions problem05.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
# Project Euler - Problem 5
# Daniel Stair

# Outer loop counts up from 1 (though it only needs to go from 2520 I think) (use i)
i = 1

while i < 240000000
# initialize our variables
j = 0
k = true

# Inner loop divides outer loop's number by each number from 1 to 20 (use j)
while j < 21 and k == true
k = i.to_f % j.to_f == 0
j += 1

if j == 20 and k == true
puts i.to_i.to_s + " PASSES"
end
end
# increment by 20 since we know the answer has to be divisible by 20
i += 20
end
35 changes: 35 additions & 0 deletions problem08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Project Euler - Problem 8
# Daniel Stair


testnums = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450".to_s


puts "Iterating through " + testnums.length.to_s + " numbers"

# brute force method
i = 0
j = 0
maxproduct = 0
max_i = 0

# loop through length of string
while i <= 100
product = \
testnums[i].to_i*testnums[i+1].to_i*testnums[i+2].to_i*testnums[i+3].to_i* \
testnums[i+4].to_i*testnums[i+5].to_i*testnums[i+6].to_i*testnums[i+7].to_i*\
testnums[i+8].to_i*testnums[i+9].to_i*testnums[i+10].to_i*testnums[i+11].to_i*\
testnums[i+12].to_i
puts product

if product > maxproduct
maxproduct = product
max_i = i
end

i += 1
end

puts "the end max product is " + maxproduct.to_s
puts "this occurred starting at the " + max_i.to_s + "th number"
puts "sequence is: " + testnums[max_i..max_i+12]