Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
43
Task/Deceptive-numbers/ALGOL-W/deceptive-numbers.alg
Normal file
43
Task/Deceptive-numbers/ALGOL-W/deceptive-numbers.alg
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#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)
|
||||
38
Task/Deceptive-numbers/Bc/deceptive-numbers.bc
Normal file
38
Task/Deceptive-numbers/Bc/deceptive-numbers.bc
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* 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
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
||||
uint64_t power_modulus(uint64_t base, uint64_t exponent, const uint64_t& modulus) {
|
||||
if ( modulus == 1 ) {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
|||
base %= modulus;
|
||||
uint64_t result = 1;
|
||||
while ( exponent > 0 ) {
|
||||
if ( ( exponent & 1 ) == 1 ) {
|
||||
if ( ( exponent & 1 ) == 1 ) {
|
||||
result = ( result * base ) % modulus;
|
||||
}
|
||||
base = ( base * base ) % modulus;
|
||||
|
|
@ -20,7 +20,7 @@ uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool is_deceptive(uint32_t n) {
|
||||
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 ) {
|
||||
if ( n % divisor == 0 || n % ( divisor + 4 ) == 0 ) {
|
||||
|
|
|
|||
|
|
@ -1,23 +1,24 @@
|
|||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned modpow(unsigned b, unsigned e, unsigned m)
|
||||
uint32_t modpow(uint32_t b, uint32_t e, uint32_t m)
|
||||
{
|
||||
unsigned p;
|
||||
uint32_t p;
|
||||
for (p = 1; e; e >>= 1) {
|
||||
if (e & 1)
|
||||
p = p * b % m;
|
||||
b = b * b % m;
|
||||
p = (uint64_t)p * b % m;
|
||||
b = (uint64_t)b * b % m;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
int is_deceptive(unsigned n)
|
||||
int is_deceptive(uint32_t n)
|
||||
{
|
||||
unsigned x;
|
||||
if (n & 1 && n % 3 && n % 5) {
|
||||
uint32_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;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -25,10 +26,11 @@ int is_deceptive(unsigned n)
|
|||
|
||||
int main(void)
|
||||
{
|
||||
unsigned c, i = 49;
|
||||
for (c = 0; c != 50; ++i) {
|
||||
if (is_deceptive(i)) {
|
||||
printf(" %u", i);
|
||||
uint32_t n = 49;
|
||||
unsigned int c;
|
||||
for (c = 0; c != 500; ++n) {
|
||||
if (is_deceptive(n)) {
|
||||
printf(" %" PRIu32, n);
|
||||
++c;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
38
Task/Deceptive-numbers/Lua/deceptive-numbers.lua
Normal file
38
Task/Deceptive-numbers/Lua/deceptive-numbers.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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
|
||||
|
|
@ -9,9 +9,9 @@ let is_deceptive n =
|
|||
let rec loop x =
|
||||
x * x <= n && (n mod x = 0 || n mod (x + 4) = 0 || loop (x + 6))
|
||||
in
|
||||
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 && loop 7 &&
|
||||
modpow n 10 (pred n) = 1
|
||||
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 &&
|
||||
modpow n 10 (pred n) = 1 && loop 7
|
||||
|
||||
let () =
|
||||
Seq.(ints 49 |> filter is_deceptive |> take 500
|
||||
Seq.(ints 49 |> filter is_deceptive |> take 100
|
||||
|> iter (Printf.printf " %u%!")) |> print_newline
|
||||
|
|
|
|||
22
Task/Deceptive-numbers/Phix/deceptive-numbers-2.phix
Normal file
22
Task/Deceptive-numbers/Phix/deceptive-numbers-2.phix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">100</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">showlim</span><span style="color: #0000FF;">=</span><span style="color: #000000;">70</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d deceptive numbers are:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">showlim</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #7060A8;">powmod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">showlim</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %7d%n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %d%s is %d\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
|
||||
<!--
|
||||
|
|
@ -7,4 +7,4 @@ def is_deceptive(n):
|
|||
if not (n % d and n % (d + 4)): return True
|
||||
return False
|
||||
|
||||
print(*islice(filter(is_deceptive, count()), 100))
|
||||
print(*islice(filter(is_deceptive, count(49)), 100))
|
||||
16
Task/Deceptive-numbers/Python/deceptive-numbers-2.py
Normal file
16
Task/Deceptive-numbers/Python/deceptive-numbers-2.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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))
|
||||
33
Task/Deceptive-numbers/UNIX-Shell/deceptive-numbers.sh
Normal file
33
Task/Deceptive-numbers/UNIX-Shell/deceptive-numbers.sh
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
15
Task/Deceptive-numbers/Wren/deceptive-numbers-2.wren
Normal file
15
Task/Deceptive-numbers/Wren/deceptive-numbers-2.wren
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue