Skip to content

Instantly share code, notes, and snippets.

@h8nor
Last active August 28, 2018 14:45
Show Gist options
  • Save h8nor/f4b61b829b42ad72128c9726fdc5dd5c to your computer and use it in GitHub Desktop.
Save h8nor/f4b61b829b42ad72128c9726fdc5dd5c to your computer and use it in GitHub Desktop.
Revision 3 calcs to n = 2**18 - 1
#require 'prime'
include Math
print "\uFEFF[#{RUBY_VERSION}]\n"
# Fixnum < 2**30 <= Bignum
n = rand(9999..375999) # 25326001 ## Memory limit exceeded
m = 2**n - 1 #! n != Bignum
def prime? num
# For larger values method performs significantly slower
# //github.com/ruby/ruby/pull/1786/
return false if num < 2
return true if num == 2 or num == 3
return false if num %2 == 0 or num %3 == 0
i = 5; w = 2
while i * i <= num
return false if num %i == 0
i += w; w = 6 - w
end
limit = sqrt(num).to_i # Float -> Fixnum
5.step(limit, 2) do |d|
return false if num %d == 0
end
true
end
#! Dividers output
def cofactors num, prime=false, div=2
factor = []
unless prime?(num)
for n in (2..num / 2)
factor << n if num %n == 0
return if div > factor[0] unless factor.empty?
factor.pop if num %n == 0 and prime unless prime?(n)
end
product = true if num == factor.inject(){|res, el| res * el}
puts "#{num} #{'co' if prime && product}factors #{factor.reverse.inspect}"
else
puts "#{num} is Prime"
end
end
#! Result
#puts "", "M#{n} is #{'not ' unless prime?m}Prime"
#! Debug
LIMIT_DIVISION = 7
n.upto(n + 60 + LIMIT_DIVISION) do |i|
cofactors i, true, LIMIT_DIVISION
#puts i.prime_division.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment