Deceptive numbers: Difference between revisions

(→‎{{header|Wren}}: Added a version which doesn't use big integers.)
(→‎bc: add)
Line 146:
3367
4141</pre>
 
=={{header|bc}}==
<syntaxhighlight lang="bc">/* modular exponentiation */
define p(b, e, m) {
auto r
for (r = 1; e > 0; e /= 2) {
if (e % 2 == 1) r = r * b % m
b = b * b % m
}
return(r)
}
 
/* cache for the primes found */
p[0] = 7
 
define d(n) {
auto i, p, r;
if (p(10, n - 1, n) == 1) {
for (r = sqrt(n); (p = p[i]) <= r; ++i) if (n % p == 0) return(1)
p[++l] = n
}
return(0)
}
 
/* wheel to skip multiples of 2, 3, and 5 */
w[0] = 2
w[1] = 4
w[2] = 2
w[3] = 4
w[4] = 6
w[5] = 2
w[6] = 6
w[7] = 4
 
for (n = 11; c != 10; i = (i + 1) % 8) {
if (d(n) == 1) {
n
c += 1
}
n += w[i]
}</syntaxhighlight>
{{out}}
<pre>
91
259
451
481
703
1729
2821
2981
3367
4141
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdintinttypes.h>
#include <stdio.h>
 
Line 180 ⟶ 234:
for (c = 0; c != 500; ++n) {
if (is_deceptive(n)) {
printf(" %u" PRIu32, n);
++c;
}
559

edits