Jump to content

Least m such that n! + m is prime: Difference between revisions

(Added Wren)
Line 27:
 
 
 
=={{header|Julia}}==
{{trans|Wren}}
<syntaxhighlight lang="julia">""" rosettacode.orgwiki/Least_m_such_that_n!_%2B_m_is_prime """
 
using Primes
 
function least_m_fact_to_prime(number_to_print, delta_limit)
fact, p, m, n, t = big"1", big"0", big"0", 0, 1000
diffs = zeros(BigInt, number_to_print)
while true
if n > 0
fact *= n
p = nextprime(fact + 1)
m = p - fact
if n < number_to_print
diffs[n] = m
end
if n == number_to_print - 1
println("Least positive m such that n! + m is prime; first $number_to_print:")
for (i, k) in enumerate(diffs)
print(lpad(k, 5), i % 10 == 0 ? "\n" : "")
end
elseif m > t
while true
print("\nFirst m > $t is $m at position $n.")
t += 1000
if m <= t
break
end
end
if t > delta_limit
return
end
end
end
n += 1
end
end
 
least_m_fact_to_prime(50, 10_000)
</syntaxhighlight>{{out}}
<pre>
Least positive m such that n! + m is prime; first 50:
1 1 1 5 7 7 11 23 17 11
1 29 67 19 43 23 31 37 89 29
31 31 97 131 41 59 1 67 223 107
127 79 37 97 61 131 1 43 97 53
1 97 71 47 239 101 233 53 83 0
 
First m > 1000 is 1069 at position 107.
First m > 2000 is 3391 at position 192.
First m > 3000 is 3391 at position 192.
First m > 4000 is 4943 at position 284.
First m > 5000 is 5233 at position 384.
First m > 6000 is 6131 at position 388.
First m > 7000 is 9067 at position 445.
First m > 8000 is 9067 at position 445.
First m > 9000 is 9067 at position 445.
First m > 10000 is 12619 at position 599.
First m > 11000 is 12619 at position 599.
First m > 12000 is 12619 at position 599.
</pre>
 
=={{header|Raku}}==
4,108

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.