Skip to content

Instantly share code, notes, and snippets.

@kylessnell
Last active December 11, 2015 00:09
Show Gist options
  • Save kylessnell/4514408 to your computer and use it in GitHub Desktop.
Save kylessnell/4514408 to your computer and use it in GitHub Desktop.
# Write a script that determines if a given number is prime
def prime_number?(num)
(2..Math.sqrt(num)).each do |n|
return false if num % n == 0
end
true
end
# Follow up: Modify the script to factor a given number into a list of constituent primes.
def constituent_primes(n)
primes_array = []
i = n/2
while i >= 1
if n % i == 0
primes_array.push(n/i)
n = i
i = n/2
else
i -= 1
end
end
primes_array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment