Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
41
Task/Truncatable-primes/Common-Lisp/truncatable-primes.lisp
Normal file
41
Task/Truncatable-primes/Common-Lisp/truncatable-primes.lisp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
(defun start ()
|
||||
(format t "Largest right-truncatable ~a~%" (max-right-truncatable))
|
||||
(format t "Largest left-truncatable ~a~%" (max-left-truncatable)))
|
||||
|
||||
(defun max-right-truncatable ()
|
||||
(loop for el in (6-digits-R-truncatables)
|
||||
maximizing el into max
|
||||
finally (return max)))
|
||||
|
||||
(defun 6-digits-R-truncatables (&optional (lst '(2 3 5 7)) (n 5))
|
||||
(if (zerop n)
|
||||
lst
|
||||
(6-digits-R-truncatables (R-trunc lst) (- n 1))))
|
||||
|
||||
(defun R-trunc (lst)
|
||||
(remove-if (lambda (x) (not (primep x)))
|
||||
(loop for el in lst
|
||||
append (mapcar (lambda (x) (+ (* 10 el) x)) '(1 3 7 9)))))
|
||||
|
||||
(defun max-left-truncatable ()
|
||||
(loop for el in (6-digits-L-truncatables)
|
||||
maximizing el into max
|
||||
finally (return max)))
|
||||
|
||||
(defun 6-digits-L-truncatables (&optional (lst '(3 7)) (n 5))
|
||||
(if (zerop n)
|
||||
lst
|
||||
(6-digits-L-truncatables (L-trunc lst (- 6 n)) (- n 1))))
|
||||
|
||||
(defun L-trunc (lst n)
|
||||
(remove-if (lambda (x) (not (primep x)))
|
||||
(loop for el in lst
|
||||
append (mapcar (lambda (x) (+ (* (expt 10 n) x) el)) '(1 2 3 4 5 6 7 8 9)))))
|
||||
|
||||
(defun primep (n)
|
||||
(primep-aux n 2))
|
||||
|
||||
(defun primep-aux (n d)
|
||||
(cond ((> d (sqrt n)) t)
|
||||
((zerop (rem n d)) nil)
|
||||
(t (primep-aux n (+ d 1)))))
|
||||
|
|
@ -38,7 +38,7 @@ public class Main {
|
|||
if(rightTrunc == -1)
|
||||
{
|
||||
int right = prime;
|
||||
while(right > 0 && primeList.get(right >> 1)) right /= 10;
|
||||
while(right > 0 && right % 2 != 0 && primeList.get(right >> 1)) right /= 10;
|
||||
if(right == 0) rightTrunc = prime;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,41 +1,10 @@
|
|||
my @primes := 2, 3, 5, -> $p { ($p+2, $p+4 ... &prime)[*-1] } ... *;
|
||||
my @isprime = False,False; # 0 and 1 are not primes by definition
|
||||
sub prime($i) { @isprime[$i] //= ($i %% none @primes ...^ * > $_ given $i.sqrt.floor) }
|
||||
constant ltp = [2, 3, 5, 7], -> @ltp {
|
||||
[ grep &is-prime, ((1..9) X~ @ltp) ]
|
||||
} ... *;
|
||||
|
||||
sub ltp {
|
||||
for 9...1 -> $a {
|
||||
for 9...1 -> $b {
|
||||
for 9...1 -> $c {
|
||||
for 9...1 -> $d {
|
||||
for 9...1 -> $e {
|
||||
for 9,7,3 -> $f {
|
||||
my @x := [\+] $f, $e, $d, $c, $b, $a Z* (1,10,100 ... *);
|
||||
return @x[*-1] if not grep {not prime $^n}, @x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
constant rtp = [2, 3, 5, 7], -> @rtp {
|
||||
[ grep &is-prime, (@rtp X~ (1..9)) ]
|
||||
} ... *;
|
||||
|
||||
sub infix:<*+> ($a,$b) { $a * 10 + $b }
|
||||
|
||||
sub rtp {
|
||||
for 7,5,3 {
|
||||
for grep &prime, ($_ X*+ 9,7,3,1) {
|
||||
for grep &prime, ($_ X*+ 9,7,3,1) {
|
||||
for grep &prime, ($_ X*+ 9,7,3,1) {
|
||||
for grep &prime, ($_ X*+ 9,7,3,1) {
|
||||
for grep &prime, ($_ X*+ 9,7,3,1) {
|
||||
return $_;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
say "Highest ltp: ", ltp;
|
||||
say "Highest rtp: ", rtp;
|
||||
say "Highest ltp = ", ltp[5][*-1];
|
||||
say "Highest rtp = ", rtp[5][*-1];
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
/*REXX pgm finds largest left- and right-truncatable primes < 1 million.*/
|
||||
parse arg high .; if high=='' then high=1000000 /*assume 1 million.*/
|
||||
/*REXX pgm finds largest left- & right-truncatable primes ≤1m (or arg1).*/
|
||||
parse arg high .; if high=='' then high=1000000 /*assume 1 million.*/
|
||||
!.=0; Lp=0; Rp=0; w=length(high) /*placeholders for primes, Lp, Rp*/
|
||||
p.1=2; p.2=3; p.3=5; p.4=7; p.5=11; p.6=13; p.7=17 /*some low primes.*/
|
||||
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1 /*low prime flags.*/
|
||||
n=7; s.n=p.n**2 /*number of primes so far. */
|
||||
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17 /*some low primes. */
|
||||
!.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1 /*low prime flags. */
|
||||
#=7; s.#=@.#**2 /*number of primes so far, prime²*/
|
||||
/*───────────────────────────────────────generate more primes ≤ high. */
|
||||
do j=p.n+2 by 2 to high /*only find odd primes from here.*/
|
||||
do j=@.#+2 by 2 to high /*only find odd primes from here.*/
|
||||
if j//3 ==0 then iterate /*is J divisible by three? */
|
||||
if right(j,1)==5 then iterate /*is the right-most digit a "5" ?*/
|
||||
if j//7 ==0 then iterate /*is J divisible by seven? */
|
||||
|
|
@ -13,29 +13,29 @@ n=7; s.n=p.n**2 /*number of primes so far. */
|
|||
if j//13 ==0 then iterate /*is J divisible by thirteen? */
|
||||
/*[↑] above five lines saves time*/
|
||||
do k=7 while s.k<=j /*divide by known odd primes. */
|
||||
if j//p.k==0 then iterate j /*Is J divisible by X? ¬ prime.*/
|
||||
if j//@.k==0 then iterate j /*Is J divisible by X? ¬ prime.*/
|
||||
end /*k*/
|
||||
n=n+1 /*bump number of primes found. */
|
||||
p.n=j; s.n=j*j /*assign to sparse array; prime².*/
|
||||
#=#+1 /*bump number of primes found. */
|
||||
@.#=j; s.#=j*j /*assign to sparse array; prime².*/
|
||||
!.j=1 /*indicate that J is a prime.*/
|
||||
end /*j*/
|
||||
/*─────────────────────────────────────find largest left truncatable P. */
|
||||
do L=n by -1 for n; if pos(0,p.L)\==0 then iterate
|
||||
do k=1 for length(p.L)-1; _=right(p.L,k) /*L truncate #.*/
|
||||
do L=# by -1 for #; if pos(0,@.L)\==0 then iterate
|
||||
do k=1 for length(@.L)-1; _=right(@.L,k) /*L truncate #.*/
|
||||
if \!._ then iterate L /*Truncated # ¬prime? Skip it.*/
|
||||
end /*k*/
|
||||
leave /*leave the DO loop, we found one*/
|
||||
end /*L*/
|
||||
/*─────────────────────────────────────find largest right truncatable P.*/
|
||||
do R=n by -1 for n; if pos(0,p.R)\==0 then iterate
|
||||
do k=1 for length(p.R)-1; _=left(p.R,k) /*R truncate #.*/
|
||||
do R=# by -1 for #; if pos(0,@.R)\==0 then iterate
|
||||
do k=1 for length(@.R)-1; _=left(@.R,k) /*R truncate #.*/
|
||||
if \!._ then iterate R /*Truncated # ¬prime? Skip it.*/
|
||||
end /*k*/
|
||||
leave /*leave the DO loop, we found one*/
|
||||
end /*R*/
|
||||
/*───────────────────────────────────────show largest left/right trunc P*/
|
||||
say 'The last prime found is ' p.n " (there are" n 'primes ≤' high")."
|
||||
say 'The last prime found is ' @.# " (there are" # 'primes ≤' high")."
|
||||
say copies('─',70) /*show a separator line. */
|
||||
say 'The largest left-truncatable prime ≤' high " is " right(p.L,w)
|
||||
say 'The largest right-truncatable prime ≤' high " is " right(p.R,w)
|
||||
say 'The largest left-truncatable prime ≤' high " is " right(@.L,w)
|
||||
say 'The largest right-truncatable prime ≤' high " is " right(@.R,w)
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue