Deceptive numbers: Difference between revisions

m
(→‎{{header|Phix}}: added without bigint version)
 
(27 intermediate revisions by 11 users not shown)
Line 64:
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601
</pre>
 
=={{header|ALGOL W}}==
Based on the second Wren sample - finds the numbers without needing large integers - see the Talk page.
<syntaxhighlight lang="algolw">
begin % find deceptive numbers - repunits R(n) evenly divisible by composite %
% numbers and n+1; see the task talk page based on the second Wren sample %
 
% returns true if n is an odd prime, false otherwise, uses trial division %
logical procedure isOddPrime ( integer value n ) ;
begin
logical prime;
integer f, f2, toNext;
prime := true;
f := 3;
f2 := 9;
toNext := 16; % note: ( 2n + 1 )^2 - ( 2n - 1 )^2 = 8n %
while f2 <= n and prime do begin
prime := n rem f not = 0;
f := f + 2;
f2 := toNext;
toNext := toNext + 8
end while_f2_le_n_and_prime ;
prime
end isOddPrime ;
 
begin % -- task %
integer n, count;
count := 0;
n := 47;
while begin n := n + 2;
count < 25
end
do begin
if n rem 3 not = 0 and n rem 5 not = 0 and not isOddPrime( n ) then begin
integer mp;
mp := 10;
for p := 2 until n - 1 do mp := ( mp * 10 ) rem n;
if mp = 1 then begin
count := count + 1;
writeon( i_w := 5, s_w := 0, " ", n );
if count rem 10 = 0 then write()
end if_mp_eq_1
end if_have_a_candidate
end while_count_lt_50
end task
 
end.
</syntaxhighlight>
{{out}}
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141
4187 5461 6533 6541 6601 7471 7777 8149 8401 8911
10001 11111 12403 13981 14701
</pre>
 
=={{header|Amazing Hopper}}==
{{trans|C}}
<syntaxhighlight lang="c">
#include <jambo.h>
 
#prototype isdeceptive(_X_)
#prototype modulepow(_X_,_Y_,_Z_)
#synon _isdeceptive Isdeceptive
#synon _modulepow ModulePow
#define Breaking Goto(exit)
Main
i = 49, c=0
Iterator ( ++i, #(c <> 10), \
Print only if ( Is deceptive 'i', Set 'i,"\n"'; ++c ) )
End
 
Subrutines
 
is deceptive ( n )
x=7
And( Bitand(n,1), And( Mod(n,3), Mod(n,5) )), do {
Iterator( x+=6, #( (x*x) <= n ),\
#(!( (n%x) && (n%(x+4)) )), do{ \
Module Pow (10, Minus one(n), n), Is equal to '1', Breaking } )
}
Set '0'
exit:
Return
 
module pow(b, e, m)
Loop for (p = 1, e, e >>= 1)
Bitand(e, 1), do{ #( p = (p * b) % m ) }
#( b = (b * b) % m )
Next
Return (p)
 
</syntaxhighlight>
{{out}}
<pre>
91
259
451
481
703
1729
2821
2981
3367
4141
</pre>
 
Line 95 ⟶ 200:
3367
4141</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">function modpow(b, e, m, r) {
for (r = 1; e > 0; e = int(e / 2)) {
if (e % 2 == 1)
r = r * b % m
b = b * b % m
}
return r
}
 
function is_pseudo(n, i, p, r) {
if (modpow(10, n - 1, n) == 1) {
r = int(sqrt(n))
while ((p = primes[++i]) <= r)
if (n % p == 0)
return 1
primes[++l] = n
}
return 0
}
 
BEGIN {
primes[l = 1] = n = 7
split("4 2 4 2 4 6 2 6", wheel)
 
do if (is_pseudo(n += wheel[i = i % 8 + 1])) {
printf " %u", n
++c
} while (c != 50)
print
}</syntaxhighlight>
{{out}}
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973</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] = 4
w[1] = 2
w[2] = 4
w[3] = 2
w[4] = 4
w[5] = 6
w[6] = 2
w[7] = 6
 
for (n = p[0]; c != 10; i = (i + 1) % 8) {
if (d(n += w[i]) == 1) {
n
c += 1
}
}</syntaxhighlight>
{{out}}
<pre>
91
259
451
481
703
1729
2821
2981
3367
4141
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdiointtypes.h>
#include <stdio.h>
 
unsigneduint32_t modpow(unsigneduint32_t b, unsigneduint32_t e, unsigneduint32_t m)
{
unsigneduint32_t p;
for (p = 1; e; e >>= 1) {
if (e & 1)
p = (uint64_t)p * b % m;
b = (uint64_t)b * b % m;
}
return p;
}
 
int is_deceptive(unsigneduint32_t n)
{
unsigneduint32_t x;
if (n & 1 && n % 3 && n % 5 && modpow(10, n - 1, n) == 1) {
for (x = 7; x * x <= n; x += 6) {
if (!(n % x && n % (x + 4)))
return modpow(10, n - 1, n) == 1;
}
}
Line 124 ⟶ 317:
int main(void)
{
unsigneduint32_t c, in = 49;
forunsigned (c = 0;int c != 50; ++i) {
for (c = 0; ifc (is_deceptive(i)!= 500; ++n) {
if printf("is_deceptive(n)) %u", i);{
printf(" %" PRIu32, n);
++c;
}
Line 134 ⟶ 328:
}</syntaxhighlight>
{{out}}
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973 65311 66991 67861 68101 75361 79003 82513 83119 94139 95161 97273 97681 100001 101101 101491 102173 108691 113201 115627 115921 118301 118957 122221 126217 128713 130351 131821 134821 134863 137137 137149 138481 139231 145181 147001 148417 152551 158497 162401 164761 166499 170017 172081 179881 188191 188269 188461 188501 196651 201917 216001 216931 225589 226273 229633 231337 234421 237169 237817 245491 247753 248677 250717 251251 252601 253099 269011 269569 274231 281821 286903 287749 287809 294409 298033 301081 302177 304057 314821 334153 340561 341503 346801 351809 357641 364277 366337 372731 385003 390313 391141 399001 401401 410041 413339 420343 437251 451091 455971 458641 463241 463489 481601 488881 489997 491063 492101 497377 497503 497927 505363 507529 509971 511969 512461 520801 522349 530881 532171 552721 567721 585631 586993 588193 589537 595231 597871 601657 603961 607321 632641 634351 642001 658801 665401 670033 679861 710533 721801 736291 741751 748657 749521 754369 764491 765703 827281 838201 841633 847693 852841 853381 868001 873181 880237 886033 903169 906193 909709 924001 929633 963857 974611 976873 981317 989017 997633 997981 999001 1004329 1005697 1024651 1033669 1038331 1039441 1039849 1041043 1042417 1056331 1069993 1080697 1081649 1082809 1090051 1096681 1111111 1128871 1141141 1146401 1152271 1163801 1167607 1168513 1169101 1171261 1182721 1193221 1242241 1242571 1251949 1267801 1268551 1272271 1275347 1279201 1287937 1294411 1298737 1306369 1308853 1336699 1362061 1376299 1394171 1398101 1408849 1419607 1446241 1446661 1450483 1461241 1462441 1463749 1492663 1498981 1504321 1531711 1543381 1551941 1569457 1576381 1583821 1590751 1602601 1615681 1703221 1705621 1708993 1711381 1711601 1719601 1731241 1739089 1748921 1756351 1758121 1769377 1772611 1773289 1792081 1803413 1809697 1810513 1826209 1840321 1846681 1850017 1857241 1863907 1869211 1876393 1884961 1887271 1892143 1894651 1907851 1909001 1918201 1922801 1926761 1942957 1943341 1945423 1953671 1991821 2024641 2030341 2044657 2056681 2063971 2085301 2100901 2113921 2146957 2161501 2165801 2171197 2171401 2184931 2190931 2194973 2203321 2205967 2215291 2237017 2278001 2290289 2299081 2305633 2314201 2337301 2358533 2371681 2386141 2392993 2406401 2424731 2432161 2433601 2455921 2463661 2503501 2508013 2512441 2524801 2525909 2539183 2553061 2563903 2585701 2603381 2628073 2630071 2658433 2694601 2699431 2704801 2719981 2765713 2795519 2824061 2868097 2879317 2887837 2899351 2916511 2949991 2960497 2976487 2987167 3005737 3057601 3076481 3084577 3117547 3125281 3140641 3146221 3167539 3186821 3209053 3225601 3247453 3255907 3270403 3362083 3367441 3367771 3369421 3387781 3398921 3400013 3426401 3429889 3471071 3488041 3506221 3520609 3523801 3536821 3537667 3581761 3593551 3596633 3658177 3677741 3678401 3740737 3781141 3788851 3815011 3817681 3828001 3850771 3862207 3879089 3898129 3913003 3930697 3947777 3985921 4005001 4010401 4014241 4040281 4040881 4064677 4074907 4077151 4099439 4100041 4119301 4151281 4181921 4182739 4209661 4225057 4234021 4255903 4260301 4316089 4326301 4335241 4360801 4363261 4363661 4372771 4373461 4415251 4415581 4454281 4463641 4469471 4469551 4499701 4504501 4543621 4627441 4630141 4637617 4762381 4767841 4784689 4806061 4863127 4903921 4909177 4909411 4909913 4917781 5031181 5045401 5049001 5056051 5122133 5148001 5160013 5180593 5197837 5203471</pre>
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973</pre>
 
=={{header|C++}}==
Line 194 ⟶ 388:
#include <iostream>
 
uint64_t power_modulus(uint64_t base, uint64_t exponent, const uint64_t& modulus) {
if ( modulus == 1 ) {
return 0;
Line 202 ⟶ 396:
uint64_t result = 1;
while ( exponent > 0 ) {
if ( ( exponent & 1 ) == 1 ) {
result = ( result * base ) % modulus;
}
Line 211 ⟶ 405:
}
 
bool is_deceptive(const uint32_t& n) {
if ( n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && power_modulus(10, n - 1, n) == 1 ) {
for ( uint32_t divisor = 7; divisor < sqrt(n); divisor += 6 ) {
Line 245 ⟶ 439:
137149 138481 139231 145181 147001 148417 152551 158497 162401 164761
166499 170017 172081 179881 188191 188269 188461 188501 196651 201917
</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func modpow b e m .
r = 1
while e > 0
if e mod 2 = 1
r = r * b mod m
.
b = b * b mod m
e = e div 2
.
return r
.
func is_deceptive n .
if n mod 2 = 1 and n mod 3 <> 0 and n mod 5 <> 0
if modpow 10 (n - 1) n = 1
x = 7
while x * x <= n
if n mod x = 0 or n mod (x + 4) = 0
return 1
.
x += 6
.
.
.
.
while cnt < 10
n += 1
if is_deceptive n = 1
write n & " "
cnt += 1
.
.
</syntaxhighlight>
{{out}}
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141
</pre>
 
Line 291 ⟶ 525:
if Isprime(n)>1 and Divides(n,Rep(n-1)) then !!n; c:+; fi
od;</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|XPLo}}
<syntaxhighlight lang="vbnet">Function ModPow(b As Uinteger, e As Uinteger, m As Uinteger) As Uinteger
Dim As Uinteger p = 1
While e <> 0
If (e And 1) Then p = (p*b) Mod m
b = (b*b) Mod m
e Shr= 1
Wend
Return p
End Function
 
Function isDeceptive(n As Uinteger) As Uinteger
Dim As Uinteger x
If (n And 1) <> 0 Andalso (n Mod 3) <> 0 Andalso (n Mod 5) <> 0 Then
x = 7
While x*x <= n
If (n Mod x) = 0 Orelse (n Mod (x+4)) = 0 Then Return (ModPow(10, n-1, n) = 1)
x += 6
Wend
End If
Return 0
End Function
 
Dim As Uinteger c = 0, i = 49
While c <> 41 ' limit for signed 32-bit integers
If isDeceptive(i) Then
Print Using "#######"; Csng(i);
c += 1
If (c Mod 10) = 0 Then Print
End If
i += 1
Wend
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as XPLo entry.</pre>
 
=={{header|Go}}==
Line 336 ⟶ 608:
 
=={{header|J}}==
<syntaxhighlight lang="j">Rwheel=:. (10x4 #.6 2 6 4 2 4 #&1)"02
deceptivefermat=:. {{1&p: < 0 = ]10 (y&|@^) R@<: y}}"0
 
2+I.deceptive 2+i.10000
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001</syntaxhighlight>
 
For improved performance:
 
<syntaxhighlight lang="j">deceptives=: {{
r=.$k=.10x #.}.1#~j=.9
while. y>#r do.
if. 0<2|j do.
if. 0<5|j do.
if. 0=1 p:j do.
if. 0=0]j|k do.
r=. r, j
end.
end.
end.
end.
k=. 1 10x p.k
j=. j+1
end.
r
}}</syntaxhighlight>
 
_10 ]\ (#~ fermat) (#~ 0&p:) +/\ 49 , 15e4 $ wheel</syntaxhighlight>
{{out}}
<pre>
<syntaxhighlight lang="j"> deceptives 21
91 259 451 481 703 1729 2821 2981703 3367 4141 41871729 5461 6533 65412821 6601 7471 77772981 8149 8401 89113367 10001</syntaxhighlight> 4141
4187 5461 6533 6541 6601 7471 7777 8149 8401 8911
10001 11111 12403 13981 14701 14911 15211 15841 19201 21931
22321 24013 24661 27613 29341 34133 34441 35113 38503 41041
45527 46657 48433 50851 50881 52633 54913 57181 63139 63973
65311 66991 67861 68101 75361 79003 82513 83119 94139 95161
97273 97681 100001 101101 101491 102173 108691 113201 115627 115921
118301 118957 122221 126217 128713 130351 131821 134821 134863 137137
137149 138481 139231 145181 147001 148417 152551 158497 162401 164761
166499 170017 172081 179881 188191 188269 188461 188501 196651 201917
216001 216931 225589 226273 229633 231337 234421 237169 237817 245491
247753 248677 250717 251251 252601 253099 269011 269569 274231 281821
286903 287749 287809 294409 298033 301081 302177 304057 314821 334153
340561 341503 346801 351809 357641 364277 366337 372731 385003 390313
391141 399001 401401 410041 413339 420343 437251 451091 455971 458641
463241 463489 481601 488881 489997 491063 492101 497377 497503 497927
505363 507529 509971 511969 512461 520801 522349 530881 532171 552721
</pre>
 
=={{header|Java}}==
Line 494 ⟶ 761:
=={{header|langur}}==
{{trans|ALGOL 68}}
<syntaxhighlight lang="langur">val .isPrime = fn(.i) {
{{works with|langur|0.8.1}}
.i == 2 or .i > 2 and
<syntaxhighlight lang="langur">val .isPrime = f .i == 2 or .i > 2 and not any f(.x) .i div .x, pseries 2 .. .i ^/ 2
not any fn(.x) { .i div .x }, pseries 2 .. .i ^/ 2
}
 
var .nums = []
Line 501 ⟶ 770:
 
for .n = 9; len(.nums) < 10; .n += 2 {
.repunit = .repunit x* 100 + 11
if not .isPrime(.n) and .repunit div .n {
.nums = more .nums, .n
Line 554 ⟶ 823:
(91 259 451 481 703 1729 2821 2981 3367 4141)
ok
</pre>
 
=={{header|Lua}}==
Based on the second Wren sample, finds the numbers without needing large integers - see the talk page for more details.
<syntaxhighlight lang="lua">
do -- find deceptive numbers - repunits R(n) evenly divisible by composite numbers and n+1
-- see tha task talk page based on the second Wren sample
 
-- returns true if n is prime, false otherwise, uses trial division %
local function isPrime ( n )
if n < 3 then return n == 2
elseif n % 3 == 0 then return n == 3
elseif n % 2 == 0 then return false
else
local prime = true
local f, f2, toNext = 5, 25, 24
while f2 <= n and prime do
prime = n % f ~= 0
f = f + 2
f2 = toNext
toNext = toNext + 8
end
return prime
end
end
 
do -- task
local n, count = 47, 0
while count < 25 do
n = n + 2
if n % 3 ~= 0 and n % 5 ~= 0 and not isPrime( n ) then
local mp = 10
for p = 2, n - 1 do mp = ( mp * 10 ) % n end
if mp == 1 then
count = count + 1
io.write( string.format( " %5d", n ) )
if count % 10 == 0 then io.write( "\n" ) end
end
end
end
end
 
end
</syntaxhighlight>
{{out}}
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141
4187 5461 6533 6541 6601 7471 7777 8149 8401 8911
10001 11111 12403 13981 14701
</pre>
 
Line 671 ⟶ 989:
 
let () =
Seq.(ints 49 |> filter is_deceptive |> take 500100
|> iter (Printf.printf " %u%!")) |> print_newline</syntaxhighlight>
{{out}}
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973 65311 66991 67861 68101 75361 79003 82513 83119 94139 95161 97273 97681 100001 101101 101491 102173 108691 113201 115627 115921 118301 118957 122221 126217 128713 130351 131821 134821 134863 137137 137149 138481 139231 145181 147001 148417 152551 158497 162401 164761 166499 170017 172081 179881 188191 188269 188461 188501 196651 201917</pre>
<pre> 91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973 65311 66991 67861 68101 75361 79003 82513 83119 94139 95161 97273 97681 100001 101101 101491 102173 108691 113201 115627 115921 118301 118957 122221 126217 128713 130351 131821 134821 134863 137137 137149 138481 139231 145181 147001 148417 152551 158497 162401 164761 166499 170017 172081 179881 188191 188269 188461 188501 196651 201917 216001 216931 225589 226273 229633 231337 234421 237169 237817 245491 247753 248677 250717 251251 252601 253099 269011 269569 274231 281821 286903 287749 287809 294409 298033 301081 302177 304057 314821 334153 340561 341503 346801 351809 357641 364277 366337 372731 385003 390313 391141 399001 401401 410041 413339 420343 437251 451091 455971 458641 463241 463489 481601 488881 489997 491063 492101 497377 497503 497927 505363 507529 509971 511969 512461 520801 522349 530881 532171 552721 567721 585631 586993 588193 589537 595231 597871 601657 603961 607321 632641 634351 642001 658801 665401 670033 679861 710533 721801 736291 741751 748657 749521 754369 764491 765703 827281 838201 841633 847693 852841 853381 868001 873181 880237 886033 903169 906193 909709 924001 929633 963857 974611 976873 981317 989017 997633 997981 999001 1004329 1005697 1024651 1033669 1038331 1039441 1039849 1041043 1042417 1056331 1069993 1080697 1081649 1082809 1090051 1096681 1111111 1128871 1141141 1146401 1152271 1163801 1167607 1168513 1169101 1171261 1182721 1193221 1242241 1242571 1251949 1267801 1268551 1272271 1275347 1279201 1287937 1294411 1298737 1306369 1308853 1336699 1362061 1376299 1394171 1398101 1408849 1419607 1446241 1446661 1450483 1461241 1462441 1463749 1492663 1498981 1504321 1531711 1543381 1551941 1569457 1576381 1583821 1590751 1602601 1615681 1703221 1705621 1708993 1711381 1711601 1719601 1731241 1739089 1748921 1756351 1758121 1769377 1772611 1773289 1792081 1803413 1809697 1810513 1826209 1840321 1846681 1850017 1857241 1863907 1869211 1876393 1884961 1887271 1892143 1894651 1907851 1909001 1918201 1922801 1926761 1942957 1943341 1945423 1953671 1991821 2024641 2030341 2044657 2056681 2063971 2085301 2100901 2113921 2146957 2161501 2165801 2171197 2171401 2184931 2190931 2194973 2203321 2205967 2215291 2237017 2278001 2290289 2299081 2305633 2314201 2337301 2358533 2371681 2386141 2392993 2406401 2424731 2432161 2433601 2455921 2463661 2503501 2508013 2512441 2524801 2525909 2539183 2553061 2563903 2585701 2603381 2628073 2630071 2658433 2694601 2699431 2704801 2719981 2765713 2795519 2824061 2868097 2879317 2887837 2899351 2916511 2949991 2960497 2976487 2987167 3005737 3057601 3076481 3084577 3117547 3125281 3140641 3146221 3167539 3186821 3209053 3225601 3247453 3255907 3270403 3362083 3367441 3367771 3369421 3387781 3398921 3400013 3426401 3429889 3471071 3488041 3506221 3520609 3523801 3536821 3537667 3581761 3593551 3596633 3658177 3677741 3678401 3740737 3781141 3788851 3815011 3817681 3828001 3850771 3862207 3879089 3898129 3913003 3930697 3947777 3985921 4005001 4010401 4014241 4040281 4040881 4064677 4074907 4077151 4099439 4100041 4119301 4151281 4181921 4182739 4209661 4225057 4234021 4255903 4260301 4316089 4326301 4335241 4360801 4363261 4363661 4372771 4373461 4415251 4415581 4454281 4463641 4469471 4469551 4499701 4504501 4543621 4627441 4630141 4637617 4762381 4767841 4784689 4806061 4863127 4903921 4909177 4909411 4909913 4917781 5031181 5045401 5049001 5056051 5122133 5148001 5160013 5180593 5197837 5203471</pre>
 
=={{header|PARI/GP}}==
Line 917 ⟶ 1,235:
The 1000th is 24279289
4 minutes and 59s
</pre>
or under p2js:
<pre>
The 100th is 201917
0.4s
</pre>
 
Line 961 ⟶ 1,284:
 
=={{header|Python}}==
;By using a generic test<nowiki>:</nowiki>
<syntaxhighlight lang="python">from itertools import count, islice
from math import isqrt
Line 970 ⟶ 1,294:
return False
 
print(*islice(filter(is_deceptive, count(49)), 100))</syntaxhighlight>
;Optimized variant to efficiently generate a sequence<nowiki>:</nowiki>
This uses a wheel to skip the multiples of 2, 3, and 5. The found primes are cached to speedup the full primality test.
<syntaxhighlight lang="python">from itertools import accumulate, cycle, islice
from math import isqrt
 
primes = []
wheel = 4, 2, 4, 2, 4, 6, 2, 6
 
def is_pseudo(n):
if pow(10, n - 1, n) == 1:
s = isqrt(n)
for p in primes:
if p > s: break
if n % p == 0: return True
primes.append(n)
return False
 
print(*islice(filter(is_pseudo, accumulate(cycle(wheel), initial=7)), 100))</syntaxhighlight>
{{out}}
<pre>91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973 65311 66991 67861 68101 75361 79003 82513 83119 94139 95161 97273 97681 100001 101101 101491 102173 108691 113201 115627 115921 118301 118957 122221 126217 128713 130351 131821 134821 134863 137137 137149 138481 139231 145181 147001 148417 152551 158497 162401 164761 166499 170017 172081 179881 188191 188269 188461 188501 196651 201917</pre>
Line 995 ⟶ 1,337:
{{out}}
<pre>91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701</pre>
 
=={{header|RPL}}==
Based on the hints given on the Talk page.
{{works with|HP|49}}
« { } 49
'''WHILE''' OVER SIZE 10 < '''REPEAT'''
'''IF''' DUP 3 MOD OVER 5 MOD AND '''THEN'''
'''IF''' DUP ISPRIME? NOT '''THEN'''
DUP MODSTO 10 OVER 1 - POWMOD
'''IF''' 1 == '''THEN''' SWAP OVER + SWAP '''END'''
'''END END'''
2 +
'''END''' DROP
» '<span style="color:blue">TASK</span>' STO
Runs in 4 minutes on a HP-50g.
 
'''With a wheel:'''
« { 4 6 2 6 4 2 4 2 } DUP SIZE → wheel wsize
« { } 49
'''WHILE''' OVER SIZE 10 < '''REPEAT'''
1 wsize '''FOR''' j
'''IF''' DUP ISPRIME? NOT '''THEN'''
DUP MODSTO 10 OVER 1 - POWMOD
'''IF''' 1 == '''THEN''' SWAP OVER + SWAP '''END'''
'''END'''
wheel j GET +
'''NEXT'''
'''END''' DROP
» » '<span style="color:blue">TASK</span>' STO
Runs in 1:26 minutes on a HP-50g.
{{out}}
<pre>
1: { 91 259 451 481 703 1729 2821 2981 3367 4141 }
</pre>
 
=={{header|Ruby}}==
Line 1,097 ⟶ 1,473:
<pre>
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001 11111 12403 13981 14701 14911 15211 15841 19201 21931 22321 24013 24661 27613 29341 34133 34441 35113 38503 41041 45527 46657 48433 50851 50881 52633 54913 57181 63139 63973 65311 66991 67861 68101 75361 79003 82513 83119 94139 95161 97273 97681 100001 101101 101491 102173 108691 113201 115627 115921 118301 118957 122221 126217 128713 130351 131821 134821 134863 137137 137149 138481 139231 145181 147001 148417 152551 158497 162401 164761 166499 170017 172081 179881 188191 188269 188461 188501 196651 201917
</pre>
 
=={{header|UNIX Shell}}==
Works with [[POSIX]]-compatible shells.
<syntaxhighlight lang="sh">is () {
return "$((!($1)))"
}
 
fermat_test () {
set -- 1 "$1" "$(($2 - 1))" "$2"
while is "$3 > 0"
do
set -- "$(($1 * (-($3 & 1) & ($2 ^ 1) ^ 1) % $4))" "$(($2 * $2 % $4))" "$(($3 >> 1))" "$4"
done
return "$(($1 != 1))"
}
 
set -- 7
c=0 n=$1
while :
do
for w in 4 2 4 2 4 6 2 6
do
fermat_test 10 "$((n += w))" && for p
do
is 'p * p > n' && {
set -- "$@" "$n"
break
}
is 'n % p == 0' && {
echo "$n"
is '(c += 1) == 10' && exit
break
}
done
done
done</syntaxhighlight>
{{out}}
<pre>
91
259
451
481
703
1729
2821
2981
3367
4141
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="go">import math.big
import math.big
 
fn is_prime(n int) bool {
if n < 2 {return false}
else if n % 2 == 0 {return falsen == 2}
} else if n %2 3 == 0 {return n == 3}
else {
return n == 2
} else if n%3 == 0 {
return n == 3
} else {
mut d := 5
for d * d <= n {
if n % d == 0 {return false}
return false
}
d += 2
if n % d == 0 {return false}
return false
}
d += 4
}
Line 1,137 ⟶ 1,556:
mut deceptive := []i64{}
for count < limit {
if !is_prime(int(n)) && n % 3 != 0 && n % 5 != 0 {
bn := big.integer_from_i64(n)
t = repunit % bn
Line 1,166 ⟶ 1,585:
 
The first 62 deceptive numbers (up to 97681 though not shown in full) are found in 0.179 seconds.
<syntaxhighlight lang="ecmascriptwren">/* deceptive_numbersDeceptive_numbers.wren */
 
import "./gmp" for Mpz
Line 1,194 ⟶ 1,613:
[91, 259, 451, 481, 703, 1729, 2821, 2981, 3367, 4141, 4187, 5461, 6533, 6541, 6601, 7471, 7777, 8149, 8401, 8911, 10001, 11111, 12403, 13981, 14701]
</pre>
 
As discussed in the talk page, it is also possible to do this task without using big integers and the following version runs under Wren-cli albeit somewhat slower at 0.103 seconds for the first 25 deceptive numbers and 0.978 seconds for the first 62. The output is the same as the GMP version.
<syntaxhighlight lang="wren">import "./math" for Int
 
var count = 0
var limit = 25 // or 62
var n = 49
var deceptive = []
while (count < limit) {
if (!Int.isPrime(n) && n % 3 != 0 && n % 5 != 0 && Int.modPow(10, n-1, n) == 1) {
deceptive.add(n)
count = count + 1
}
n = n + 2
}
System.print("The first %(limit) deceptive numbers are:")
System.print(deceptive)</syntaxhighlight>
 
=={{header|XPL0}}==
885

edits