Skip to content

Instantly share code, notes, and snippets.

@FifthSurprise
Created March 7, 2014 15:13
Show Gist options
  • Save FifthSurprise/9413242 to your computer and use it in GitHub Desktop.
Save FifthSurprise/9413242 to your computer and use it in GitHub Desktop.
Euler3 and Euler4
def euler3
num=600851475143
n=num
if n % 2 ==0
lastFactor = 2
n = n/2
while n%2==0
n = n/2
end
else
lastFactor = 1
end
currentFactor = 3
maxFactor = Math.sqrt(num)
while n>1 && currentFactor<=maxFactor
if n % currentFactor ==0
n = n/currentFactor
lastFactor = currentFactor
while n%currentFactor==0
n=n/currentFactor
end
maxFactor = Math.sqrt(n)
end
currentFactor = currentFactor+2
end
final = (n==1) ? lastFactor : n
end
def euler4
largestPalindrome = 0
a = 100
while a <= 999
b = a
while b <=999
largestPalindrome = a*b if palindrome?(a*b) && a*b>largestPalindrome
b = b+1
end
a = a+1
end
largestPalindrome
end
def palindrome?(num)
(num).to_s.reverse.to_i == (num)
end
def sieve(num)
arr = Array.new(num,true)
arr[0]=false
arr[1]=false
arr.each_index do |x|
if arr[x] && x>1
(x+1..arr.length).each do |a|
(arr[a] = false) if arr[a] && a % x ==0
end
end
end
return arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment