Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,68 @@
## Generic helper functions
def count(s): reduce s as $x (0; .+1);
# counting from 0
def enumerate(s): foreach s as $x (-1; .+1; [., $x]);
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
## Prime numbers
def is_prime:
if . == 2 then true
else
2 < . and . % 2 == 1 and
(. as $in
| (($in + 1) | sqrt) as $m
| [false, 3] | until( .[0] or .[1] > $m; [$in % .[1] == 0, .[1] + 2])
| .[0]
| not)
end ;
## Cyclops numbers
def iscyclops:
(tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| ($l % 2 == 1) and $d[$m] == 48 and count($d[] | select(.== 48)) == 1 # "0"
;
# Generate a stream of cyclops numbers, in increasing numeric order, from 0
def cyclops:
# generate a stream of cyclops numbers with $n digits on each side of the central 0
def w:
if . == 0 then ""
else (.-1)|w as $left
| $left + (range(1;10)|tostring)
end;
def c: w as $left | $left + "0" + w;
range(0; infinite) | c | tonumber;
# Generate a stream of palindromic cyclops numbers, in increasing numeric order, from 0
def palindromiccyclops:
def r: explode|reverse|implode;
def c: . as $n
| if $n == 0 then "0"
elif $n == 1
then (range(1;10)|tostring) as $base
| $base + "0" + ($base | r)
else (range(pow(10;$n-1); pow(10; $n))|tostring|select(test("0")|not)) as $base
| $base + "0" + ($base | r)
end;
range(0; infinite) | c | tonumber;
# check that a cyclops number minus the 0 is prime
def cyclops_isblind:
(tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| ((( $d[:$m] + $d[$m+1:] ) | implode | tonumber) | is_prime);
# check that a cyclops number is a palindrome
def cyclops_ispalindromic:
. as $in
| (tostring | explode) as $d
| ($d|length) as $l
| (($l + 1) / 2 - 1) as $m
| $d[:$m] == ( $d[$m+1:] | reverse) ;

View file

@ -0,0 +1,34 @@
def print5x10($width):
. as $a
| range(0;5) as $i
| reduce range(0;10) as $j (""; . + ($a[10*$i + $j] | lpad($width)));
def main_task($n):
"The first \($n) cyclops numbers are:",
([limit($n; cyclops)] | print5x10(7)),
"\nThe first \($n) prime cyclops numbers are:",
([limit($n; cyclops | select(is_prime))] | print5x10(7)),
"\nThe first \($n) blind prime cyclops numbers are:",
([limit($n; cyclops | select(is_prime and cyclops_isblind))] | print5x10(7)),
"\nThe first \($n) palindromic prime cyclops numbers are:",
([limit($n; cyclops | select(cyclops_ispalindromic and is_prime))] | print5x10(8)) ;
def stretch_task($big):
def pp: " \(.[1]) at index \(.[0]).";
"\nFirst cyclops greater than \($big):" +
(first(enumerate(cyclops) | select(.[1] > $big))|pp),
"\nThe next prime cyclops number after \($big):" +
(first(enumerate(cyclops | select(is_prime)) | select(.[1] | (. > $big)) ) | pp),
"\nThe first blind prime cyclops number greater than \($big):" +
(first(enumerate(cyclops | select(is_prime and cyclops_isblind)) | select(.[1] > $big) )|pp),
"\nThe first palindromic prime cyclops number greater than \($big):" +
(first(enumerate(palindromiccyclops | select(is_prime)) | select(.[1] > $big) ) | pp) ;
main_task(50),
stretch_task(pow(10;7))