Skip to content

Instantly share code, notes, and snippets.

@daniel-g
Created August 9, 2010 18:02
Show Gist options
  • Save daniel-g/515807 to your computer and use it in GitHub Desktop.
Save daniel-g/515807 to your computer and use it in GitHub Desktop.
def get_triangle_number(divisors)
puts Time.now
number = 1
found = false
until found
first_number_primes = get_primes(number)
second_number_primes = get_primes(number + 1)
primes = first_number_primes | second_number_primes
triangle_number = ( number * (number + 1) ) / 2
prime_times = times_each_prime(triangle_number, primes)
divisor_count = 1
prime_times.each{|k| divisor_count = divisor_count * (k + 1) }
if divisor_count >= divisors
found = true
else
number = number + 1
end
end
puts Time.now
puts triangle_number
end
def get_primes(number)
primes = []
for posible_prime in 2..number
prime_is_posible = true
primes.each{|prime| prime_is_posible = false if posible_prime%prime == 0 }
if prime_is_posible && number%posible_prime == 0
primes << posible_prime
end
end
primes
end
def times_each_prime(number, primes)
times = []
try_number = number
for prime in primes
still_more = true
count = 0
while still_more == true && try_number > 1
if try_number%prime == 0
count = count + 1
try_number = try_number / prime
else
still_more = false
end
end
times << count
end
times
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment