Curzon numbers: Difference between revisions

Content added Content deleted
(Python: much faster with modpow)
(→‎{{header|Ruby}}: Switched to modular pow like Python)
Line 1,356: Line 1,356:
<syntaxhighlight lang="ruby">def curzons(k)
<syntaxhighlight lang="ruby">def curzons(k)
Enumerator.new do |y|
Enumerator.new do |y|
(1..).each {|n| y << n if (k ** n + 1) % (k * n + 1) == 0 }
(1..).each do |n|
m = k * n + 1
y << n if k.pow(n, m ) + 1 == m
end
end
end
end
end


[2,4,6,8,10].each do |base|
[2,4,6,8,10].each do |base|
puts "Curzon numbers with k = #{base}:"
puts "Curzon numbers with k = #{base}:"
puts curzons(base).take(50).join(", ")
puts curzons(base).take(50).join(", ")
puts "Thousandth Curzon with k = #{base}: #{curzons(base).find.each.with_index(1){|_,i| i == 1000} }",""
puts "Thousandth Curzon with k = #{base}: #{curzons(base).find.each.with_index(1){|_,i| i == 1000} }",""
end</syntaxhighlight>
end</syntaxhighlight>


Line 1,386: Line 1,389:
Thousandth Curzon with k = 10: 46845.
Thousandth Curzon with k = 10: 46845.
</pre>
</pre>

=={{header|Rust}}==
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn modpow(mut base: usize, mut exp: usize, n: usize) -> usize {
<syntaxhighlight lang="rust">fn modpow(mut base: usize, mut exp: usize, n: usize) -> usize {