Super-Poulet numbers: Difference between revisions

(Added Swift solution)
Line 311:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
class Integer
def proper_divisors
return [] if self == 1
primes = prime_division.flat_map{|prime, freq| [prime] * freq}
(1...primes.size).each_with_object([1]) do |n, res|
primes.combination(n).map{|combi| res << combi.inject(:*)}
end.flatten.uniq.sort
end
end
 
super_poulets = (1..).lazy.select do |n|
n.prime? == false &&
2.pow(n-1, n) == 1 && # modular exponentiation
n.proper_divisors[1..].all?{|d| 2.pow(d, d) == 2} # again
end
 
m = 20
puts "First #{m} super-Poulet numbers:\n#{super_poulets.first(m).join(-", ") }"
 
[1_000_000, 10_000_000].each do |m|
puts "\nValue and index of first super-Poulet number greater than #{m}:"
puts "%d is #%d" % super_poulets.with_index(1).detect{|n, i| n > m }
end</syntaxhighlight>
{{out}}
<pre>First 20 super-Poulet numbers:
341, 1387, 2047, 2701, 3277, 4033, 4369, 4681, 5461, 7957, 8321, 10261, 13747, 14491, 15709, 18721, 19951, 23377, 31417, 31609
 
Value and index of first super-Poulet greater than 1000000:
1016801 is #109
 
Value and index of first super-Poulet greater than 10000000:
10031653 is #317
</pre>
=={{header|Swift}}==
{{trans|C++}}
1,149

edits