Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Deceptive-numbers/00-META.yaml
Normal file
2
Task/Deceptive-numbers/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Deceptive_numbers
|
||||
38
Task/Deceptive-numbers/00-TASK.txt
Normal file
38
Task/Deceptive-numbers/00-TASK.txt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
Repunits are numbers that consist entirely of repetitions of the digit one (unity). The notation '''R<sub>n</sub>''' symbolizes the repunit made up of '''n''' ones.
|
||||
|
||||
Every prime '''p''' larger than 5, evenly divides the repunit '''R<sub>p-1</sub>'''.
|
||||
|
||||
|
||||
;E.G.
|
||||
|
||||
The repunit '''R<sub>6</sub>''' is evenly divisible by '''7'''.
|
||||
|
||||
<span style=font-size:125%;font-weight:bold;padding-left:3em;>111111 / 7 = 15873</span>
|
||||
|
||||
The repunit '''R<sub>42</sub>''' is evenly divisible by '''43'''.
|
||||
|
||||
<span style=font-size:125%;font-weight:bold;padding-left:3em;>111111111111111111111111111111111111111111 / 43 = 2583979328165374677002583979328165374677</span>
|
||||
|
||||
And so on.
|
||||
|
||||
|
||||
There are composite numbers that also have this same property. They are often referred to as ''deceptive non-primes'' or ''deceptive numbers''.
|
||||
|
||||
|
||||
The repunit '''R<sub>90</sub>''' is evenly divisible by the composite number '''91''' (=7*13).
|
||||
|
||||
<div style=font-size:125%;font-weight:bold;padding-left:3em;>111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 / 91 = 1221001221001221001221001221001221001221001221001221001221001221001221001221001221001221</div>
|
||||
|
||||
|
||||
;Task
|
||||
|
||||
* Find and show at least the first '''10 deceptive numbers'''; composite numbers '''n''' that evenly divide the repunit '''R<sub>n-1</sub>'''
|
||||
|
||||
|
||||
;See also
|
||||
|
||||
;* [https://www.numbersaplenty.com/set/deceptive_number Numbers Aplenty - Deceptive numbers]
|
||||
;* [[oeis:A000864|OEIS:A000864 - Deceptive nonprimes: composite numbers k that divide the repunit R_{k-1}]]
|
||||
<br>
|
||||
|
||||
|
||||
18
Task/Deceptive-numbers/ALGOL-68/deceptive-numbers.alg
Normal file
18
Task/Deceptive-numbers/ALGOL-68/deceptive-numbers.alg
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
BEGIN # find repunits (all digits are 1 ) such that R(n-1) is divisible by n and n is not prime #
|
||||
# R(n) is the nth repunit, so has n 1s #
|
||||
PR precision 8000 PR # set precision of LONG LONG INT, enough for up to R(8000) #
|
||||
PR read "primes.incl.a68" PR # include prime utilities #
|
||||
[]BOOL prime = PRIMESIEVE 8000;
|
||||
LONG LONG INT repunit := 111 111; # n must be odd as all repunits are odd, the lowest odd #
|
||||
INT r count := 0; # non-prime is 9, so we start with repunit set to R(6) #
|
||||
FOR n FROM 9 BY 2 WHILE r count < 15 DO
|
||||
repunit *:= 100 +:= 11; # gets R(n-1) from R(n-3) #
|
||||
IF NOT prime[ n ] THEN
|
||||
IF repunit MOD n = 0 THEN
|
||||
# found non-prime n which divides R(n-1) #
|
||||
print( ( " ", whole( n, 0 ) ) );
|
||||
r count +:= 1
|
||||
FI
|
||||
FI
|
||||
OD
|
||||
END
|
||||
15
Task/Deceptive-numbers/Arturo/deceptive-numbers.arturo
Normal file
15
Task/Deceptive-numbers/Arturo/deceptive-numbers.arturo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
deceptive?: function [n][
|
||||
and? -> not? prime? n
|
||||
-> zero? (to :integer repeat "1" n-1) % n
|
||||
]
|
||||
|
||||
cnt: 0
|
||||
i: 3
|
||||
|
||||
while [cnt < 10][
|
||||
if deceptive? i [
|
||||
print i
|
||||
cnt: cnt + 1
|
||||
]
|
||||
i: i + 2
|
||||
]
|
||||
33
Task/Deceptive-numbers/C++/deceptive-numbers.cpp
Normal file
33
Task/Deceptive-numbers/C++/deceptive-numbers.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <gmpxx.h>
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
bool is_prime(int n) {
|
||||
if (n < 2)
|
||||
return false;
|
||||
if (n % 2 == 0)
|
||||
return n == 2;
|
||||
if (n % 3 == 0)
|
||||
return n == 3;
|
||||
for (int p = 5; p * p <= n; p += 4) {
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
p += 2;
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "First 100 deceptive numbers:\n";
|
||||
mpz_class repunit = 11;
|
||||
for (int n = 3, count = 0; count != 100; n += 2) {
|
||||
if (n % 3 != 0 && n % 5 != 0 && !is_prime(n) &&
|
||||
mpz_divisible_ui_p(repunit.get_mpz_t(), n))
|
||||
std::cout << std::setw(6) << n << (++count % 10 == 0 ? '\n' : ' ');
|
||||
repunit *= 100;
|
||||
repunit += 11;
|
||||
}
|
||||
}
|
||||
36
Task/Deceptive-numbers/C/deceptive-numbers.c
Normal file
36
Task/Deceptive-numbers/C/deceptive-numbers.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
|
||||
unsigned modpow(unsigned b, unsigned e, unsigned m)
|
||||
{
|
||||
unsigned p;
|
||||
for (p = 1; e; e >>= 1) {
|
||||
if (e & 1)
|
||||
p = p * b % m;
|
||||
b = b * b % m;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
int is_deceptive(unsigned n)
|
||||
{
|
||||
unsigned x;
|
||||
if (n & 1 && n % 3 && n % 5) {
|
||||
for (x = 7; x * x <= n; x += 6) {
|
||||
if (!(n % x && n % (x + 4)))
|
||||
return modpow(10, n - 1, n) == 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned c, i = 49;
|
||||
for (c = 0; c != 50; ++i) {
|
||||
if (is_deceptive(i)) {
|
||||
printf(" %u", i);
|
||||
++c;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
3
Task/Deceptive-numbers/F-Sharp/deceptive-numbers.fs
Normal file
3
Task/Deceptive-numbers/F-Sharp/deceptive-numbers.fs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
// Deceptive numbers. Nigel Galloway: February 13th., 2022
|
||||
Seq.unfold(fun n->Some(n|>Seq.filter(isPrime>>not)|>Seq.filter(fun n->(10I**(n-1)-1I)%(bigint n)=0I),n|>Seq.map((+)30)))(seq{1;7;11;13;17;19;23;29})|>Seq.concat|>Seq.skip 1
|
||||
|>Seq.chunkBySize 10|>Seq.take 7|>Seq.iter(fun n->n|>Array.iter(printf "%7d "); printfn "")
|
||||
11
Task/Deceptive-numbers/Factor/deceptive-numbers.factor
Normal file
11
Task/Deceptive-numbers/Factor/deceptive-numbers.factor
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
USING: io kernel lists lists.lazy math math.functions
|
||||
math.primes prettyprint ;
|
||||
|
||||
: repunit ( m -- n ) 10^ 1 - 9 / ;
|
||||
|
||||
: composite ( -- list ) 4 lfrom [ prime? not ] lfilter ;
|
||||
|
||||
: deceptive ( -- list )
|
||||
composite [ [ 1 - repunit ] keep divisor? ] lfilter ;
|
||||
|
||||
10 deceptive ltake [ pprint bl ] leach nl
|
||||
7
Task/Deceptive-numbers/Fermat/deceptive-numbers.fermat
Normal file
7
Task/Deceptive-numbers/Fermat/deceptive-numbers.fermat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Func Rep(n)=Sigma<m=0,n-1>[10^m].;
|
||||
c:=0;
|
||||
n:=3;
|
||||
while c<10 do
|
||||
n:=n+1;
|
||||
if Isprime(n)>1 and Divides(n,Rep(n-1)) then !!n; c:+; fi
|
||||
od;
|
||||
117
Task/Deceptive-numbers/Free-Pascal/deceptive-numbers.pas
Normal file
117
Task/Deceptive-numbers/Free-Pascal/deceptive-numbers.pas
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
program DeceptiveNumbers;
|
||||
{$IfDef FPC} {$Optimization ON,ALL} {$ENDIF}
|
||||
{$IfDef Windows} {$APPTYPE CONSOLE} {$ENDIF}
|
||||
uses
|
||||
sysutils;
|
||||
const
|
||||
LIMIT = 100000;//1E6 at home takes over (5 min) now 1m10s
|
||||
RepInitLen = 13; //Uint64 19 decimal digits -> max 6 digits divisor
|
||||
DecimalDigits = 10*1000*1000*1000*1000;//1E13
|
||||
RepLimit = (DecimalDigits-1)DIV 9;//RepInitLen '1'
|
||||
|
||||
type
|
||||
tmyUint64 = array[0..Limit DIV RepInitLen+1] of Uint64;
|
||||
var
|
||||
{$Align 32}
|
||||
K: tmyUint64;
|
||||
{$Align 32}
|
||||
MaxKIdx : Int32;
|
||||
|
||||
procedure OutK(const K:tmyUint64);
|
||||
var
|
||||
i : Uint32;
|
||||
begin
|
||||
For i := MaxKidx downto 0 do
|
||||
begin
|
||||
write(k[i]:13);
|
||||
end;
|
||||
writeln;
|
||||
end;
|
||||
|
||||
function isPrime(n: UInt64):boolean;
|
||||
var
|
||||
p: Uint64;
|
||||
begin
|
||||
if n in [2,3,5,7,11,13,17,19,23,29] then
|
||||
EXIT(true);
|
||||
|
||||
if Not ODD(n) OR ( n MOD 3 = 0) then
|
||||
EXIT(false);
|
||||
p := 5;
|
||||
repeat
|
||||
if (n mod p=0)or(n mod(p+2)=0) then
|
||||
EXIT(false);
|
||||
p +=6;
|
||||
until p*p>n;
|
||||
Exit(true);
|
||||
end;
|
||||
|
||||
procedure ExtendRep(var K:tmyUint64;n:NativeUint);
|
||||
var
|
||||
q : Uint64;
|
||||
i : Int32;
|
||||
begin
|
||||
n -= MaxKidx*RepInitLen;
|
||||
i := MaxKidx;
|
||||
while RepInitLen<=n do
|
||||
begin
|
||||
K[i] := RepLimit;
|
||||
inc(i);
|
||||
dec(n,RepInitLen);
|
||||
end;
|
||||
if n = 0 then
|
||||
Exit;
|
||||
MaxKidx := i;
|
||||
q := 1;
|
||||
while n<RepInitLen do
|
||||
begin
|
||||
q *= 10;
|
||||
inc(n);
|
||||
end;
|
||||
K[i] := RepLimit DIV q;
|
||||
end;
|
||||
|
||||
function GetModK(const K:tmyUint64;n:Uint64):NativeUint;
|
||||
var
|
||||
r,q : Uint64;
|
||||
i : Uint32;
|
||||
Begin
|
||||
r := 0;
|
||||
For i := MaxKidx downto 0 do
|
||||
begin
|
||||
q := K[i]+r*DecimalDigits;
|
||||
r := q MOD n;
|
||||
end;
|
||||
Exit(r)
|
||||
end;
|
||||
|
||||
const
|
||||
NextNotMulOF35 : array[0..7] of byte = (6,4,2,4,2,4,6,2);
|
||||
var
|
||||
i,cnt,idx35 : UInt64;
|
||||
BEGIN
|
||||
fillchar(K,SizeOF(K),#0);
|
||||
MaxKIdx:= 0;
|
||||
cnt := 0;
|
||||
i := 1;
|
||||
idx35 := 0;
|
||||
repeat
|
||||
inc(i,NextNotMulOF35[idx35]);
|
||||
IF i > LIMIT then
|
||||
BREAK;
|
||||
idx35 := (idx35+1) AND 7;
|
||||
if isprime(i) then
|
||||
continue;
|
||||
ExtendRep(k,i-1);
|
||||
IF GetModK(K,i)=0 then
|
||||
Begin
|
||||
inc(cnt);
|
||||
write(i:6,',');
|
||||
if cnt Mod 10 = 0 then
|
||||
writeln;
|
||||
end;
|
||||
until false;
|
||||
{$IfDef Windows}
|
||||
readln;
|
||||
{$ENDIF}
|
||||
END.
|
||||
33
Task/Deceptive-numbers/Go/deceptive-numbers.go
Normal file
33
Task/Deceptive-numbers/Go/deceptive-numbers.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"rcu"
|
||||
)
|
||||
|
||||
func main() {
|
||||
count := 0
|
||||
limit := 25
|
||||
n := int64(17)
|
||||
repunit := big.NewInt(1111111111111111)
|
||||
t := new(big.Int)
|
||||
zero := new(big.Int)
|
||||
eleven := big.NewInt(11)
|
||||
hundred := big.NewInt(100)
|
||||
var deceptive []int64
|
||||
for count < limit {
|
||||
if !rcu.IsPrime(int(n)) && n%3 != 0 && n%5 != 0 {
|
||||
bn := big.NewInt(n)
|
||||
if t.Rem(repunit, bn).Cmp(zero) == 0 {
|
||||
deceptive = append(deceptive, n)
|
||||
count++
|
||||
}
|
||||
}
|
||||
n += 2
|
||||
repunit.Mul(repunit, hundred)
|
||||
repunit.Add(repunit, eleven)
|
||||
}
|
||||
fmt.Println("The first", limit, "deceptive numbers are:")
|
||||
fmt.Println(deceptive)
|
||||
}
|
||||
5
Task/Deceptive-numbers/J/deceptive-numbers-1.j
Normal file
5
Task/Deceptive-numbers/J/deceptive-numbers-1.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
R=: (10x #. #&1)"0
|
||||
deceptive=: 1&p: < 0 = ] | R@<:
|
||||
|
||||
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
|
||||
17
Task/Deceptive-numbers/J/deceptive-numbers-2.j
Normal file
17
Task/Deceptive-numbers/J/deceptive-numbers-2.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
}}
|
||||
2
Task/Deceptive-numbers/J/deceptive-numbers-3.j
Normal file
2
Task/Deceptive-numbers/J/deceptive-numbers-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
deceptives 21
|
||||
91 259 451 481 703 1729 2821 2981 3367 4141 4187 5461 6533 6541 6601 7471 7777 8149 8401 8911 10001
|
||||
28
Task/Deceptive-numbers/Jq/deceptive-numbers.jq
Normal file
28
Task/Deceptive-numbers/Jq/deceptive-numbers.jq
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def is_prime:
|
||||
. as $n
|
||||
| if ($n < 2) then false
|
||||
elif ($n % 2 == 0) then $n == 2
|
||||
elif ($n % 3 == 0) then $n == 3
|
||||
elif ($n % 5 == 0) then $n == 5
|
||||
elif ($n % 7 == 0) then $n == 7
|
||||
elif ($n % 11 == 0) then $n == 11
|
||||
elif ($n % 13 == 0) then $n == 13
|
||||
elif ($n % 17 == 0) then $n == 17
|
||||
elif ($n % 19 == 0) then $n == 19
|
||||
else 23
|
||||
| until( (. * .) > $n or ($n % . == 0); .+2)
|
||||
| . * . > $n
|
||||
end;
|
||||
|
||||
# Output: a stream
|
||||
def deceptives:
|
||||
{nextrepunit: 1111111111111111}
|
||||
| foreach range(17; infinite; 2) as $n (.;
|
||||
.repunit = .nextrepunit
|
||||
| .nextrepunit |= . * 100 + 11;
|
||||
select( ($n | is_prime | not)
|
||||
and ($n % 3 != 0) and ($n % 5 != 0)
|
||||
and (.repunit % $n == 0 ))
|
||||
| $n );
|
||||
|
||||
"The first 25 deceptive numbers are:", [limit(25;deceptives)]
|
||||
13
Task/Deceptive-numbers/Julia/deceptive-numbers.julia
Normal file
13
Task/Deceptive-numbers/Julia/deceptive-numbers.julia
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using Primes
|
||||
|
||||
function deceptives(numwanted)
|
||||
n, r, ret = 2, big"1", Int[]
|
||||
while length(ret) < numwanted
|
||||
!isprime(n) && r % n == 0 && push!(ret, n)
|
||||
n += 1
|
||||
r = 10r + 1
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
@time println(deceptives(30))
|
||||
29
Task/Deceptive-numbers/LFE/deceptive-numbers.lfe
Normal file
29
Task/Deceptive-numbers/LFE/deceptive-numbers.lfe
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(defmodule deceptives
|
||||
(export (prime? 1) (deceptives 1)))
|
||||
|
||||
(defun prime? (n)
|
||||
(if (< n 2)
|
||||
'false
|
||||
(prime? n 2 0 #B(1 2 2 4 2 4 2 4 6 2 6))))
|
||||
|
||||
(defun prime? (n d j wheel)
|
||||
(cond
|
||||
((=:= j (byte_size wheel))
|
||||
(prime? n d 3 wheel))
|
||||
((> (* d d) n)
|
||||
'true)
|
||||
((=:= 0 (rem n d))
|
||||
'false)
|
||||
(else
|
||||
(prime? n (+ d (binary:at wheel j)) (+ j 1) wheel))))
|
||||
|
||||
(defun deceptives (n)
|
||||
(deceptives 2 1 n '()))
|
||||
|
||||
(defun deceptives
|
||||
((_ _ 0 l)
|
||||
(lists:reverse l))
|
||||
((k r n l)
|
||||
(if (andalso (not (prime? k)) (=:= 0 (rem r k)))
|
||||
(deceptives (+ k 1) (+ (* r 10) 1) (- n 1) (cons k l))
|
||||
(deceptives (+ k 1) (+ (* r 10) 1) n l))))
|
||||
13
Task/Deceptive-numbers/Langur/deceptive-numbers.langur
Normal file
13
Task/Deceptive-numbers/Langur/deceptive-numbers.langur
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
val .isPrime = f .i == 2 or .i > 2 and not any f(.x) .i div .x, pseries 2 .. .i ^/ 2
|
||||
|
||||
var .nums = []
|
||||
var .repunit = 111_111
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
writeln .nums
|
||||
15
Task/Deceptive-numbers/Mathematica/deceptive-numbers.math
Normal file
15
Task/Deceptive-numbers/Mathematica/deceptive-numbers.math
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
ClearAll[DeceptiveNumberQ]
|
||||
DeceptiveNumberQ[n_Integer] := If[! PrimeQ[n], PowerMod[10, n - 1, 9 n] == 1]
|
||||
c = 0;
|
||||
out = Reap[Do[
|
||||
If[DeceptiveNumberQ[i],
|
||||
Sow[i];
|
||||
c++;
|
||||
If[c >= 1000, Break[]]
|
||||
]
|
||||
,
|
||||
{i, 2, \[Infinity]}
|
||||
]][[2, 1]];
|
||||
Print["The first 100:"]
|
||||
Multicolumn[Take[out, 100], Appearance -> "Horizontal"]
|
||||
Print["The 1000th is: ", out[[1000]]]
|
||||
31
Task/Deceptive-numbers/Nim/deceptive-numbers.nim
Normal file
31
Task/Deceptive-numbers/Nim/deceptive-numbers.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import std/[math, strutils]
|
||||
|
||||
func pow(a, n: Natural; m: Positive): Natural =
|
||||
var a = a mod m
|
||||
var n = n
|
||||
if a > 0:
|
||||
result = 1
|
||||
while n > 0:
|
||||
if (n and 1) != 0:
|
||||
result = (result * a) mod m
|
||||
n = n shr 1
|
||||
a = (a * a) mod m
|
||||
|
||||
func sqrt(n: Natural): Natural = Natural(sqrt(float(n)))
|
||||
|
||||
func isDeceptive(n: Natural): bool =
|
||||
if (n and 1) != 0 and n mod 3 != 0 and n mod 5 != 0 and pow(10, n - 1, n) == 1:
|
||||
for d in countup(7, sqrt(n), 6):
|
||||
if n mod d == 0 or n mod (d + 4) == 0:
|
||||
return true
|
||||
result = false
|
||||
|
||||
var count = 0
|
||||
var n = 7
|
||||
while true:
|
||||
if n.isDeceptive:
|
||||
inc count
|
||||
stdout.write align($n, 6)
|
||||
stdout.write if count mod 10 == 0: '\n' else: ' '
|
||||
if count == 100: break
|
||||
inc n
|
||||
17
Task/Deceptive-numbers/OCaml/deceptive-numbers.ocaml
Normal file
17
Task/Deceptive-numbers/OCaml/deceptive-numbers.ocaml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
let modpow m =
|
||||
let rec loop p b e =
|
||||
if e land 1 = 0
|
||||
then if e = 0 then p else loop p (b * b mod m) (e lsr 1)
|
||||
else loop (p * b mod m) (b * b mod m) (e lsr 1)
|
||||
in loop 1
|
||||
|
||||
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
|
||||
|
||||
let () =
|
||||
Seq.(ints 49 |> filter is_deceptive |> take 500
|
||||
|> iter (Printf.printf " %u%!")) |> print_newline
|
||||
4
Task/Deceptive-numbers/PARI-GP/deceptive-numbers.parigp
Normal file
4
Task/Deceptive-numbers/PARI-GP/deceptive-numbers.parigp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Rep(n)=sum(X=0,n-1,10^X)
|
||||
c=0
|
||||
n=4
|
||||
while(c<10,if(!isprime(n)&&Rep(n-1)%n==0,c=c+1;print(n));n=n+1)
|
||||
10
Task/Deceptive-numbers/Perl/deceptive-numbers.pl
Normal file
10
Task/Deceptive-numbers/Perl/deceptive-numbers.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use Math::AnyNum qw(imod is_prime);
|
||||
|
||||
my($x,@D) = 2;
|
||||
while ($x++) {
|
||||
push @D, $x if 1 == $x%2 and !is_prime $x and 0 == imod(1x($x-1),$x);
|
||||
last if 25 == @D
|
||||
}
|
||||
print "@D\n";
|
||||
24
Task/Deceptive-numbers/Phix/deceptive-numbers.phix
Normal file
24
Task/Deceptive-numbers/Phix/deceptive-numbers.phix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(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: #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: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">repunit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</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;">limit</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: #000080;font-style:italic;">-- No repunit is ever divisible by 2 or 5 since it ends in 1.
|
||||
-- If n is 3*k, sum(digits(repunit))=3*k-1, not divisible by 3.
|
||||
-- Hence only check odd and hop any multiples of 3 or 5.</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">repunit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">repunit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">repunit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">repunit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">)</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;">mpz_divisible_ui_p</span><span style="color: #0000FF;">(</span><span style="color: #000000;">repunit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</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: #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;">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;">"%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>
|
||||
<!--
|
||||
31
Task/Deceptive-numbers/Prolog/deceptive-numbers.pro
Normal file
31
Task/Deceptive-numbers/Prolog/deceptive-numbers.pro
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
checkpair(1, 2).
|
||||
checkpair(R, K) :-
|
||||
checkpair(R0, K0),
|
||||
R is 10*R0 + 1,
|
||||
K is K0 + 1.
|
||||
|
||||
deceptive(K) :-
|
||||
checkpair(R, K),
|
||||
\+ prime(K),
|
||||
divmod(R, K, _, 0).
|
||||
|
||||
task(K, Ns) :-
|
||||
lazy_findall(N, deceptive(N), Ds),
|
||||
length(Ns, K),
|
||||
prefix(Ns, Ds).
|
||||
|
||||
% check if a number is prime
|
||||
%
|
||||
wheel235(L) :-
|
||||
W = [4, 2, 4, 2, 4, 6, 2, 6 | W],
|
||||
L = [1, 2, 2 | W].
|
||||
|
||||
prime(N) :-
|
||||
N >= 2,
|
||||
wheel235(W),
|
||||
prime(N, 2, W).
|
||||
|
||||
prime(N, D, _) :- D*D > N, !.
|
||||
prime(N, D, [A|As]) :-
|
||||
N mod D =\= 0,
|
||||
D2 is D + A, prime(N, D2, As).
|
||||
10
Task/Deceptive-numbers/Python/deceptive-numbers.py
Normal file
10
Task/Deceptive-numbers/Python/deceptive-numbers.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from itertools import count, islice
|
||||
from math import isqrt
|
||||
|
||||
def is_deceptive(n):
|
||||
if n & 1 and n % 3 and n % 5 and pow(10, n - 1, n) == 1:
|
||||
for d in range(7, isqrt(n) + 1, 6):
|
||||
if not (n % d and n % (d + 4)): return True
|
||||
return False
|
||||
|
||||
print(*islice(filter(is_deceptive, count()), 100))
|
||||
2
Task/Deceptive-numbers/Raku/deceptive-numbers.raku
Normal file
2
Task/Deceptive-numbers/Raku/deceptive-numbers.raku
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
my \R = [\+] 1, 10, 100 … *;
|
||||
put (2..∞).grep( {$_ % 2 && $_ % 3 && $_ % 5 && !.is-prime} ).grep( { R[$_-2] %% $_ } )[^25];
|
||||
14
Task/Deceptive-numbers/Ruby/deceptive-numbers.rb
Normal file
14
Task/Deceptive-numbers/Ruby/deceptive-numbers.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
require 'prime'
|
||||
|
||||
deceptives = Enumerator.new do |y|
|
||||
10.step(by: 10) do |n|
|
||||
[1,3,7,9].each do |digit|
|
||||
cand = n + digit
|
||||
next if cand % 3 == 0 || cand.prime?
|
||||
repunit = ("1"*(cand-1)).to_i
|
||||
y << cand if (repunit % cand) == 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
p deceptives.take(25).to_a
|
||||
25
Task/Deceptive-numbers/Rust/deceptive-numbers.rust
Normal file
25
Task/Deceptive-numbers/Rust/deceptive-numbers.rust
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// [dependencies]
|
||||
// primal = "0.3"
|
||||
// rug = "1.15.0"
|
||||
|
||||
fn main() {
|
||||
println!("First 100 deceptive numbers:");
|
||||
use rug::Integer;
|
||||
let mut repunit = Integer::from(11);
|
||||
let mut n: u32 = 3;
|
||||
let mut count = 0;
|
||||
while count != 100 {
|
||||
if n % 3 != 0 && n % 5 != 0 && !primal::is_prime(n as u64) && repunit.is_divisible_u(n) {
|
||||
print!("{:6}", n);
|
||||
count += 1;
|
||||
if count % 10 == 0 {
|
||||
println!();
|
||||
} else {
|
||||
print!(" ");
|
||||
}
|
||||
}
|
||||
n += 2;
|
||||
repunit *= 100;
|
||||
repunit += 11;
|
||||
}
|
||||
}
|
||||
18
Task/Deceptive-numbers/Scheme/deceptive-numbers.ss
Normal file
18
Task/Deceptive-numbers/Scheme/deceptive-numbers.ss
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(define prime?
|
||||
(let ((wheel '(1 2 2 . #1=(4 2 4 2 4 6 2 6 . #1#))))
|
||||
(lambda (n)
|
||||
(if (< n 2)
|
||||
#f
|
||||
(let loop ((f 2) (w wheel))
|
||||
(cond
|
||||
((> (* f f) n) #t)
|
||||
((zero? (remainder n f)) #f)
|
||||
(#t (loop (+ f (car w)) (cdr w)))))))))
|
||||
|
||||
(define (deceptives n)
|
||||
(let loop ((k 2) (r 1) (n n) (l '()))
|
||||
(if (zero? n)
|
||||
(reverse! l)
|
||||
(if (and (not (prime? k)) (zero? (remainder r k)))
|
||||
(loop (+ k 1) (+ (* 10 r) 1) (- n 1) (cons k l))
|
||||
(loop (+ k 1) (+ (* 10 r) 1) n l)))))
|
||||
3
Task/Deceptive-numbers/Sidef/deceptive-numbers.sidef
Normal file
3
Task/Deceptive-numbers/Sidef/deceptive-numbers.sidef
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
say 100.by {|n|
|
||||
n.is_composite && (divmod(powmod(10, n-1, n)-1, 9, n) == 0)
|
||||
}.join(' ')
|
||||
51
Task/Deceptive-numbers/V-(Vlang)/deceptive-numbers.v
Normal file
51
Task/Deceptive-numbers/V-(Vlang)/deceptive-numbers.v
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import math.big
|
||||
|
||||
fn is_prime(n int) bool {
|
||||
if n < 2 {
|
||||
return false
|
||||
} else if n%2 == 0 {
|
||||
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
|
||||
}
|
||||
d += 2
|
||||
if n%d == 0 {
|
||||
return false
|
||||
}
|
||||
d += 4
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut count := 0
|
||||
limit := 25
|
||||
mut n := i64(17)
|
||||
mut repunit := big.integer_from_i64(1111111111111111)
|
||||
mut t := big.integer_from_int(0)
|
||||
zero := big.integer_from_int(0)
|
||||
eleven := big.integer_from_int(11)
|
||||
hundred := big.integer_from_int(100)
|
||||
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
|
||||
if t == zero {
|
||||
deceptive << n
|
||||
count++
|
||||
}
|
||||
}
|
||||
n += 2
|
||||
repunit = repunit * hundred
|
||||
repunit = repunit + eleven
|
||||
}
|
||||
println("The first $limit deceptive numbers are:")
|
||||
println(deceptive)
|
||||
}
|
||||
22
Task/Deceptive-numbers/Wren/deceptive-numbers.wren
Normal file
22
Task/Deceptive-numbers/Wren/deceptive-numbers.wren
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* deceptive_numbers.wren */
|
||||
|
||||
import "./gmp" for Mpz
|
||||
import "./math" for Int
|
||||
|
||||
var count = 0
|
||||
var limit = 25
|
||||
var n = 17
|
||||
var repunit = Mpz.from(1111111111111111)
|
||||
var deceptive = []
|
||||
while (count < limit) {
|
||||
if (!Int.isPrime(n) && n % 3 != 0 && n % 5 != 0) {
|
||||
if (repunit.isDivisibleUi(n)) {
|
||||
deceptive.add(n)
|
||||
count = count + 1
|
||||
}
|
||||
}
|
||||
n = n + 2
|
||||
repunit.mul(100).add(11)
|
||||
}
|
||||
System.print("The first %(limit) deceptive numbers are:")
|
||||
System.print(deceptive)
|
||||
37
Task/Deceptive-numbers/XPL0/deceptive-numbers.xpl0
Normal file
37
Task/Deceptive-numbers/XPL0/deceptive-numbers.xpl0
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
func ModPow(B, E, M);
|
||||
int B, E, M, P;
|
||||
[P:= 1;
|
||||
while E # 0 do
|
||||
[if E & 1 then
|
||||
P:= rem(P*B/M);
|
||||
B:= rem(B*B/M);
|
||||
E:= E >> 1;
|
||||
];
|
||||
return P;
|
||||
];
|
||||
|
||||
func IsDeceptive(N);
|
||||
int N, X;
|
||||
[if (N&1) # 0 and rem(N/3) # 0 and rem(N/5) # 0 then
|
||||
[X:= 7;
|
||||
while X*X <= N do
|
||||
[if rem(N/X) = 0 or rem(N/(X+4)) = 0 then
|
||||
return ModPow(10, N-1, N) = 1;
|
||||
X:= X + 6;
|
||||
];
|
||||
];
|
||||
return false;
|
||||
];
|
||||
|
||||
int C, I;
|
||||
[Format(7, 0);
|
||||
I:= 49; C:= 0;
|
||||
while C # 41 do \limit for signed 32-bit integers
|
||||
[if IsDeceptive(I) then
|
||||
[RlOut(0, float(I));
|
||||
C:= C+1;
|
||||
if rem(C/10) = 0 then CrLf(0);
|
||||
];
|
||||
I:= I+1;
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue