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,2 @@
---
from: http://rosettacode.org/wiki/Sisyphus_sequence

View file

@ -0,0 +1,40 @@
The '''Sisyphus sequence''' is an infinite sequence of positive integers that was devised in 2022 by Eric Angelini and Carole Dubois.
The first term is 1. Subsequent terms are found by applying the following rule:
* If the previous term was even, then halve it.
* If the previous term was odd, then add the smallest prime number that has not yet been added.
1 is odd and so the second term is: 1 + 2 = 3, because 2 is the smallest prime not yet added.
3 is odd and so the third term is: 3 + 3 = 6, because 3 is the smallest prime not yet added.
6 is even and so the fourth term is : 6 ÷ 2 = 3, and so on.
;Task
Find and show on this page (in 10 lines of 10 terms), the first 100 terms of the sequence.
What are the '''1,000'''th, '''10,000'''th, '''100,000'''th and '''1,000,000'''th terms of the sequence and, in each case, what is the highest prime needed to reach them?
If it is difficult or impossible for your language or output device to meet all of these requirements, then just do what you reasonably can.
;Stretch
What are the '''10 millionth''' and '''100 millionth''' terms and the highest prime needed to reach each one?
By the time the '''100 millionth''' term is reached, which number(s) '''under 250''':
* Have not yet occurred in the sequence.
* Have occurred the most times and their number of occurrences.
;Extreme stretch
What is the number of the first term to equal 36?
This was originally set as a challenge by Neil Sloane who was worried by its non-appearance and found by Russ Cox.
;References
* OEIS sequence [[oeis:A350877|A350877: The Sisyphus sequence]]
<br>

View file

@ -0,0 +1,100 @@
BEGIN # generate elements of the Sysiphus Sequence: see OEIS A350877 #
# returns the largest element in a #
OP MAX = ( []INT a )INT:
IF LWB a >UPB a THEN 0
ELSE
INT result := a[ LWB a ];
FOR i FROM LWB a + 1 TO UPB a DO
IF result < a[ i ] THEN result := a[ i ] FI
OD;
result
FI # MAX # ;
# sieve the primes #
INT sieve max = 1 000 000 000; ### CHANGE TO E.G.: 100 000 000 FOR ALGOL 68G ###
BOOL is odd := TRUE;
[ sieve max ]BOOL sieve; FOR i TO UPB sieve DO sieve[ i ] := is odd; is odd := NOT is odd OD;
sieve[ 1 ] := FALSE;
sieve[ 2 ] := TRUE;
FOR s FROM 3 BY 2 TO ENTIER sqrt( sieve max ) DO
IF sieve[ s ] THEN
FOR p FROM s * s BY s TO sieve max DO sieve[ p ] := FALSE OD
FI
OD;
[ 1 : 250 ]INT pos; # positions of 1..250 in the sequence #
[ 1 : 250 ]INT occurs; # occurances of 1..250 in the sequence #
FOR i TO UPB pos DO pos[ i ] := occurs[ i ] := 0 OD;
INT max count = sieve max OVER 10; # highest element required #
INT s := 1; # the first element is defined as 1 #
INT count := 1; # count of elements found so far #
print( ( "Sysiphus sequence - first 100 elements:", newline ) );
print( ( whole( s, -4 ) ) );
pos[ s ] := count;
INT next to show := 1000; # next power-of-10 element to show #
INT last used prime := 0; # latest prime from the list #
INT p pos := 0; # current position in the sieve #
WHILE count < max count DO
# calculate the next element #
IF NOT ODD s THEN
# the previous element was even - halve it #
s OVERAB 2
ELSE
# the previous element was odd: add the next prime from the list #
WHILE p pos +:= 1;
NOT sieve[ p pos ]
DO SKIP OD;
s +:= ( last used prime := p pos )
FI;
count +:= 1;
IF count <= 100 THEN # have one of the first 100 elements #
print( ( whole( s, -4 ) ) );
IF count MOD 10 = 0 THEN print( ( newline ) ) FI;
IF count = 100 THEN print( ( newline ) ) FI
ELIF count = next to show THEN
# reached a power of ten count #
print( ( "sequence element ", whole( count, -10 )
, " is ", whole( s, -10 )
, ", highest used prime is ", whole( last used prime, -10 )
, newline
)
);
next to show *:= 10
FI;
IF s < UPB pos THEN
IF pos[ s ] = 0 THEN
# have the first appearence of s in the sequence #
pos[ s ] := count
FI;
occurs[ s ] +:= 1
FI
OD;
print( ( newline ) );
print( ( "Integers in 1..", whole( UPB pos, 0 )
, " not found in the sequence up to element ", whole( max count, 0 )
, ":", newline
)
);
FOR i TO UPB pos DO
IF pos[ i ] = 0 THEN print( ( " ", whole( i, 0 ) ) ) FI
OD;
print( ( newline ) );
INT max occurs = MAX occurs;
print( ( "Integers in 1..", whole( UPB pos, 0 )
, " that occur most often ( ", whole( max occurs, 0 )
, " times ) up to element ", whole( max count, 0 )
, ":", newline
)
);
FOR i TO UPB occurs DO
IF occurs[ i ] = max occurs THEN print( ( " ", whole( i, 0 ) ) ) FI
OD;
print( ( newline, newline ) );
print( ( "Position in the sequence of 1..100 up to element ", whole( max count, 0 )
, ":", newline
)
);
FOR i TO 100 DO
print( ( IF pos[ i ] = 0 THEN " unknown" ELSE whole( pos[ i ], -8 ) FI ) );
IF i MOD 8 = 0 THEN print( ( newline ) ) FI
OD
END

View file

@ -0,0 +1,13 @@
sisyphuseq=: {{
r=. 1
P=: _1
while. y>#r do. p=. {:P
if. 2|N=. {:r do.
P=: P, p=. 1+p
r=. r,N+p:p
else.
P=: P, p
r=. r,-:N
end.
end.
}}

View file

@ -0,0 +1,17 @@
seq=: sisyphuseq 1e6
10 10$seq NB. first 100 members of sequence
1 3 6 3 8 4 2 1 8 4
2 1 12 6 3 16 8 4 2 1
18 9 28 14 7 30 15 44 22 11
42 21 58 29 70 35 78 39 86 43
96 48 24 12 6 3 62 31 92 46
23 90 45 116 58 29 102 51 130 65
148 74 37 126 63 160 80 40 20 10
5 106 53 156 78 39 146 73 182 91
204 102 51 178 89 220 110 55 192 96
48 24 12 6 3 142 71 220 110 55
x:(,. (seq {~ <:),. P p:@{~ <:) 1e3 1e4 1e5 1e6 NB. nth elements of sequence and corresponding largest prime used
1000 990 2273
10000 24975 30713
100000 265781 392111
1000000 8820834 4761697

View file

@ -0,0 +1,26 @@
# The def of _nwise can be omitted if using the C implementation of jq.
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
# tabular print
def tprint(columns; wide):
reduce _nwise(columns) as $row ("";
. + ($row|map(lpad(wide)) | join(" ")) + "\n" );
# Input: a positive integer
# Output: an array, $a, of length .+1 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase($i):
if .[$i] then
reduce (range(2*$i; length; $i)) as $j (.; .[$j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i)) ;

View file

@ -0,0 +1,42 @@
# $limit is the target number of Sisyphus numbers to find and should be at least 100
def task($limit):
((7 * $limit) | primeSieve | map(select(.))) as $primes
| { under250: [0, 1],
sisyphus: [1],
prev: 1,
nextPrimeIndex: 0,
specific: 1000,
count:1 }
| while(.count <= $limit;
.emit = null
| if .prev % 2 == 0 then .next = .prev/2
else .next = .prev + $primes[.nextPrimeIndex]
| .nextPrimeIndex += 1
end
| .count += 1
| if .count <= 100 then .sisyphus += [.next] else . end
| if .next < 250 then .under250[.next] += 1 else . end
| if .count == 100
then .emit = "The first 100 members of the Sisyphus sequence are:\n" + (.sisyphus | tprint(10;3))
elif .count == .specific
then $primes[.nextPrimeIndex-1] as $prime
| .emit = "\(.count|lpad(8))th member is: \(.next|lpad(10)) and highest prime needed: \($prime|lpad(10))"
| .specific *= 10
else .
end
| .prev = .next )
# The results:
| select(.emit).emit,
if .count == $limit
then .under250 as $u
| [range(1;250) | select( $u[.] == null)] as $notFound
| ($u|max) as $max
| [range(1;250) | select($u[.] == $max)] as $maxFound
| "\nThese numbers under 250 do not occur in the first \(.count) terms:",
" \($notFound)",
"\nThese numbers under 250 occur the most in the first \(.count) terms:",
" \($maxFound) all occur \($max) times."
else empty
end;
task(1e7)

View file

@ -0,0 +1,61 @@
using Counters, Formatting, Primes
struct Sisyphus end
function Base.iterate(s::Sisyphus, state = (0, 0))
n, p = state
if n == 0
return (1, 0), (1, 0)
else
if isodd(n)
p = nextprime(p + 1)
n += p
else
n ÷= 2
end
return (n, p), (n, p)
end
end
function test_sisyphus()
coun = Counter{Int}()
println("The first 100 members of the Sisyphus sequence are:")
for (i, (n, p)) in enumerate(Sisyphus())
if n < 250
coun[n] += 1
end
if i < 101
print(rpad(n, 4), i % 10 == 0 ? "\n" : "")
elseif i in [1000, 10000, 100_000, 1_000_000, 10_000_000, 100_000_000]
print(
rpad("\n$(format(i, commas = true))th number:", 22),
rpad(format(n, commas = true), 14),
"Highest prime needed: ",
format(p, commas = true),
)
end
if i == 100_000_000
println(
"\n\nThese numbers under 250 do not occur in the first 100,000,000 terms:",
)
println(" ", filter(j -> !haskey(coun, j), 1:249), "\n")
sorteds = sort!([(p, coun[p]) for p in coun], by = last)
maxtimes = sorteds[end][2]
println(
"These numbers under 250 occur the most ($maxtimes times) in the first 100,000,000 terms:",
)
println(" ", map(first, filter(p -> p[2] == maxtimes, sorteds)))
elseif n == 36
println("\nLocating the first entry in the sequence with value 36:")
println(
" The sequence position: ",
format(i, commas = true),
" has value $n using prime ",
format(p, commas = true),
)
break
end
end
end
test_sisyphus()

View file

@ -0,0 +1,89 @@
import std/[bitops, math, strformat, strutils, tables]
# Sieve which uses only one bit for odd integers.
type Sieve = object
data: seq[byte]
func `[]`(sieve: Sieve; idx: Positive): bool =
## Return value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
result = sieve.data[iByte].testBit(iBit)
func `[]=`(sieve: var Sieve; idx: Positive; val: bool) =
## Set value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
if val: sieve.data[iByte].setBit(iBit)
else: sieve.data[iByte].clearBit(iBit)
func initSieve(lim: Positive): Sieve =
## Initialize a sieve from 2 to "lim".
result.data = newSeq[byte]((lim + 16) shr 4)
result[1] = true
for n in countup(3, sqrt(lim.toFloat).int, 2):
if not result[n]:
for k in countup(n * n, lim, 2 * n):
result[k] = true
func isPrime(sieve: Sieve; n: int): bool =
## Return true if "n" is prime.
result = if (n and 1) == 0: n == 2 else: not sieve[n]
let sieve = initSieve(700_000_000)
func nextPrime(sieve: Sieve; n: int): int =
## Return next prime greater than "n".
result = n
while true:
inc result
if sieve.isPrime(result):
return
var p = 0 # Current prime (none for now).
var count = 0 # Count of elements in sequence.
var n = 1 # Last element of sequence.
var counts: CountTable[int] # Count of occurrences for value < 250.
echo "First 100 terms of the Sisyphus sequence:"
while count < 100_000_000:
# Process current number.
inc count
if count <= 100:
stdout.write &"{n:3}"
stdout.write if count mod 10 == 0: '\n' else: ' '
if count == 100: echo()
elif count in [1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000]:
echo &"The {insertSep($count)}th term is {insertSep($n)} " &
&"and the highest prime needed is {insertSep($p)}."
# Update count table.
if n < 250: counts.inc(n)
# Find next term of the sequence.
if (n and 1) == 0:
n = n shr 1
else:
p = sieve.nextPrime(p)
inc n, p
echo()
echo "Numbers under 250 which dont occur in the first 100_000_000 terms:"
for n in 1..249:
if n notin counts:
stdout.write " ", n
echo '\n'
echo "Numbers under 250 which occur the most in the first 100_000_000 terms:"
counts.sort()
let largest = counts.largest[1]
for (n, count) in counts.pairs:
if count == largest:
stdout.write " ", n
else:
# No more value with the largest number of occurrences.
echo()
break

View file

@ -0,0 +1,35 @@
use strict;
use warnings;
use feature 'say';
use ntheory 'next_prime';
use List::Util <any max>;
use integer;
sub comma { reverse ((reverse shift) =~ s/.{3}\K/,/gr) =~ s/^,//r }
sub table { my $t = 10 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
my ($exp1, $exp2, $limit1, $limit2) = (3, 8, 100, 250);
my ($n, $s0, $s1, $p, @S1, %S, %S2) = (1, 1, 0, 1, 1);
my @Nth = map { 10**$_ } $exp1..$exp2;
do {
$n++;
$s1 = (0 == $s0%2) ? $s0/2 : $s0 + ($p = next_prime($p));
push @S1, $s1 if $n <= $limit1;
$S2{$s1}++ if $s1 <= $limit2;
($S{$n}{'value'} = $s1 and $S{$n}{'prime'} = $p) if any { $_ == $n } @Nth;
$s0 = $s1;
} until $n == $Nth[-1];
say 'The first 100 members of the Sisyphus sequence are:';
say table @S1;
printf "%12sth member is: %13s with prime: %11s\n", comma($_), comma($S{$_}{value}), comma($S{$_}{prime}) for @Nth;
printf "\nNumbers under $limit2 that do not occur in the first %s terms:\n", comma $Nth[-1];
say join ' ', grep { ! defined $S2{$_} } 1..$limit2;
my $max = max values %S2;
printf "\nNumbers under $limit2 occur the most ($max times) in the first %s terms:\n", comma $Nth[-1];
say join ' ', sort { $a <=> $b } grep { $S2{$_} == $max } keys %S2;

View file

@ -0,0 +1,35 @@
(phixonline)-->
<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;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e8</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sisyphus</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">under250</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reinstate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">250</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">specific</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">next</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">next</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">np</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">next</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">next</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">250</span> <span style="color: #008080;">then</span> <span style="color: #000000;">under250</span><span style="color: #0000FF;">[</span><span style="color: #000000;">next</span><span style="color: #0000FF;">]</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: #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;">100</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">sisyphus</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">next</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">100</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 first 100 members of the Sisyphus sequence are:\n%s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sisyphus</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">specific</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;">"%,13d%s member is: %,13d and highest prime needed: %,11d\n"</span><span style="color: #0000FF;">,</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;">next</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">largest</span><span style="color: #0000FF;">(</span><span style="color: #000000;">under250</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">);</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">specific</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</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;">"\nThese numbers under 250 do not occur in the first %,d terms:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</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;">" %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">under250</span><span style="color: #0000FF;">)})</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;">"\nThese numbers under 250 occur the most in the first %,d terms:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</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;">" %v all occur %d times.\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">find_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">under250</span><span style="color: #0000FF;">),</span><span style="color: #000000;">m</span><span style="color: #0000FF;">})</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>
<!--

View file

@ -0,0 +1,28 @@
(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">primesieve</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (can provide if desired, ask me on either my or this tasks talk page)</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #004600;">WINDOWS</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #000000;">64</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</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: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">36</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">next</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">next</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primesieve_next_prime</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">next</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">cpc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">/</span><span style="color: #000000;">77_534_485_877</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">ppc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">/</span><span style="color: #000000;">677_121_348_413</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">100</span>
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count: %,d (%2.2f%%) p: %,d (%2.2f%%)\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cpc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ppc</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;">while</span>
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</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;">"%,d%s member is: %,d and highest prime needed: %,d\n"</span><span style="color: #0000FF;">,</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;">next</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">})</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>
<!--

View file

@ -0,0 +1,73 @@
from collections import Counter
from itertools import islice
from typing import Iterable
from typing import Iterator
from typing import Tuple
from typing import TypeVar
import primesieve
def primes() -> Iterator[int]:
it = primesieve.Iterator()
while True:
yield it.next_prime()
def sisyphus() -> Iterator[Tuple[int, int]]:
prime = primes()
n = 1
p = 0
yield n, p
while True:
if n % 2:
p = next(prime)
n = n + p
else:
n = n // 2
yield n, p
def consume(it: Iterator[Tuple[int, int]], n) -> Tuple[int, int]:
next(islice(it, n - 1, n - 1), None)
return next(it)
T = TypeVar("T")
def batched(it: Iterable[T], n: int) -> Iterable[Tuple[T, ...]]:
_it = iter(it)
batch = tuple(islice(_it, n))
while batch:
yield batch
batch = tuple(islice(_it, n))
if __name__ == "__main__":
it = sisyphus()
first_100 = list(islice(it, 100))
print("The first 100 members of the Sisyphus sequence are:")
for row in batched(first_100, 10):
print(" ".join(str(n).rjust(3) for n, _ in row))
print("")
for interval in [10**x for x in range(3, 9)]:
n, prime = consume(it, interval - (interval // 10))
print(f"{interval:11,}th number: {n:13,} highest prime needed: {prime:11,}")
print("")
sisyphus_lt_250 = Counter(n for n, _ in islice(sisyphus(), 10**8) if n < 250)
print("These numbers under 250 do not occur in the first 100,000,000 terms:")
print(" ", [n for n in range(1, 250) if n not in sisyphus_lt_250])
print("")
most_common = sisyphus_lt_250.most_common(1)[0][1]
print("These numbers under 250 occur the most in the first 100,000,000 terms:")
print(
f" {[n for n, c in sisyphus_lt_250.items() if c == most_common]} "
f"all occur {most_common} times."
)

View file

@ -0,0 +1,22 @@
import primesieve
def sisyphus36():
primes = primesieve.Iterator()
n = 1
p = 0
i = 1
while True:
i += 1
if n % 2:
p = primes.next_prime()
n = n + p
else:
n = n // 2
if n == 36:
print(f"{i:,}, {n:,}, {p:,}")
break
if __name__ == "__main__":
sisyphus36()

View file

@ -0,0 +1,25 @@
use Math::Primesieve;
use Lingua::EN::Numbers;
my ($exp1, $exp2, $limit1, $limit2) = 3, 8, 100, 250;
my ($n, $s0, $s1, $p, @S1, %S) = 1, 1, Any, Any, 1;
my $iterator = Math::Primesieve::iterator.new;
my @Nth = ($exp1..$exp2)».exp(10);
my $S2 = BagHash.new;
repeat {
$n++;
$s1 = $s0 %% 2 ?? $s0 div 2 !! $s0 + ($p = $iterator.next);
@S1.push: $s1 if $n $limit1;
$S2.add: $s1 if $s1 $limit2;
%S{$n}{'value', 'prime'} = $s1, $p if $n @Nth;
$s0 = $s1;
} until $n == @Nth[*-1];
say "The first $limit1 members of the Sisyphus sequence are:";
say @S1.batch(10)».fmt('%4d').join("\n") ~ "\n";
printf "%12sth member is: %13s with prime: %11s\n", ($_, %S{$_}{'value'}, %S{$_}{'prime'})».&comma for @Nth;
say "\nNumbers under $limit2 that do not occur in the first {comma @Nth[*-1]} terms:";
say (1..$limit2).grep: * $S2.keys;
say "\nNumbers under $limit2 that occur the most ({$S2.values.max} times) in the first {comma @Nth[*-1]} terms:";
say $S2.keys.grep({ $S2{$_} == $S2.values.max}).sort;

View file

@ -0,0 +1,44 @@
import "./math" for Int, Nums
import "./fmt" for Fmt
var limit = 1e8
var primes = Int.primeSieve(7 * limit)
var under250 = List.filled(250, 0)
var sisyphus = [1]
under250[1] = 1
var prev = 1
var nextPrimeIndex = 0
var specific = 1000
var count = 1
while (true) {
var next
if (prev % 2 == 0) {
next = prev / 2
} else {
next = prev + primes[nextPrimeIndex]
nextPrimeIndex = nextPrimeIndex + 1
}
count = count + 1
if (count <= 100) sisyphus.add(next)
if (next < 250) under250[next] = under250[next] + 1
if (count == 100) {
System.print("The first 100 members of the Sisyphus sequence are:")
Fmt.tprint("$3d ", sisyphus, 10)
System.print()
} else if (count == specific) {
var prime = primes[nextPrimeIndex-1]
Fmt.print("$,13r member is: $,13d and highest prime needed: $,11d", count, next, prime)
if (count == limit) {
var notFound = (1..249).where { |i| under250[i] == 0 }.toList
var max = Nums.max(under250)
var maxFound = (1..249).where { |i| under250[i] == max }.toList
Fmt.print("\nThese numbers under 250 do not occur in the first $,d terms:", count)
Fmt.print(" $n", notFound)
Fmt.print("\nThese numbers under 250 occur the most in the first $,d terms:", count)
Fmt.print(" $n all occur $d times.", maxFound, max)
return
}
specific = 10 * specific
}
prev = next
}

View file

@ -0,0 +1,21 @@
import "./psieve" for Primes
import "./fmt" for Fmt
var it = Primes.iter()
var n = 1
var count = 1
var prime
var target = 36
while (true) {
if (n % 2 == 1) {
prime = it.next
n = n + prime
} else {
n = n / 2
}
count = count + 1
if (n == target) {
Fmt.print("$,r member is: $d and highest prime needed: $,d", count, target, prime)
return
}
}