question:
How would you write the code to find the prime factors of a very large
number?
I think one of the factors is small (ie) less than 1000
the code I have written is:
print ’ please enter num: ’
num_one=gets.chomp.to_i
data = []
for x in (3…1000)
if x%2!=0 then
a = (x+1)/2
for i in (2…a)
if (x%i!=0)then
if i==a then
if num_one% x==x then
num_one/x=y
data <<x
print x
print y
end
end
end
end
end
end
#the large numbers given num_one and num_two are both odd
question:
How would you write the code to find the prime factors of a very large
number?
I think one of the factors is small (ie) less than 1000
the code I have written is:
##I need help in writing the code, am I on the right track?
I am not sure what you are looking for: are you looking for feedback
on your specific code or are you looking for efficient prime
factorization algorithms? In the latter, there are quire a few around
and documented, for example at
primes = []
Prime.each {|n| primes << n ; break if n > 4000 }
p primes.last
factors = Prime.prime_division(12_000)
p factors
prints
4001
[[2, 5], [3, 1], [5, 3]]
hope this helps
Ja xv wrote in post #1031499:
question:
How would you write the code to find the prime factors of a very large
number?
I think one of the factors is small (ie) less than 1000
the code I have written is:
print ’ please enter num: ’
num_one=gets.chomp.to_i
data = []
for x in (3…1000)
if x%2!=0 then
a = (x+1)/2
for i in (2…a)
if (x%i!=0)then
if i==a then
if num_one% x==x then
num_one/x=y
data <<x
print x
print y
end
end
end
end
end
end
#the large numbers given num_one and num_two are both odd
-----Messaggio originale-----
Da: Giampiero Z. [mailto:[email protected]]
Inviato: luned 14 novembre 2011 15:10
A: ruby-talk ML
Oggetto: Re: calculating the prime factors of a very large number
Marc H. wrote in post #1031759:
require “prime”
^^^ Is this specific to Ruby 1.9.x ?
Yes it is specific to 1.9
maybe you can be interested in Miller-Rabin prime test in Ruby (search
on
web)
-----Messaggio originale-----
Da: Ja xv [mailto:[email protected]]
Inviato: venerd 11 novembre 2011 23:47
A: ruby-talk ML
Oggetto: calculating the prime factors of a very large number
question:
How would you write the code to find the prime factors of a very large
number?
I think one of the factors is small (ie) less than 1000 the code I have
written is:
print ’ please enter num: ’
num_one=gets.chomp.to_i
data = []
for x in (3…1000)
if x%2!=0 then
a = (x+1)/2
for i in (2…a)
if (x%i!=0)then
if i==a then
if num_one% x==x then
num_one/x=y
data <<x
print x
print y
end
end
end
end
end
end
#the large numbers given num_one and num_two are both odd