Sisyphus sequence: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Added a description of the program.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 2 users not shown)
Line 307:
77,534,485,877th member is 36 and highest prime needed is 677,121,348,413
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
prim = 1
proc nextprim . .
repeat
prim += 1
until isprim prim = 1
.
.
numfmt 0 4
n = 1
write n
for i = 2 to 100
if n mod 2 <> 0
nextprim
n += prim
else
n /= 2
.
write n
if i mod 10 = 0
print ""
.
.
</syntaxhighlight>
 
=={{header|J}}==
Line 344 ⟶ 383:
 
=={{header|Java}}==
Using a segmented prime sieveiterator enables the extreme stretch task to be completed in approximately 70 minutes without using any external libraries.
<syntaxhighlight lang="java">
import java.util.ArrayList;
Line 374 ⟶ 413:
} else if ( count == target ) {
target *= 10;
System.out.println(String.format("%11d%s%13d11d%s%11d10d",
target, "th member is ", next, " and highest prime needed is ", iterator.getPrime()));
}
Line 436 ⟶ 475:
final class SegmentedPrimeIterator {
public SegmentedPrimeIterator(long aLimitlimit) {
squareRoot = (int) Math.sqrt(aLimitlimit);
high = squareRoot;
smallSieve(squareRoot);
Line 476 ⟶ 515:
}
private void smallSieve(int aSquareRootsquareRoot) {
boolean[] markedPrime = new boolean[aSquareRootsquareRoot + 1];
Arrays.fill(markedPrime, true);
for ( int p = 2; p * p <= aSquareRootsquareRoot; p++ ) {
if ( markedPrime[p] ) {
for ( int i = p * p; i <= aSquareRootsquareRoot; i += p ) {
markedPrime[i] = false;
}
Line 488 ⟶ 527:
}
for ( int p = 2; p <= aSquareRootsquareRoot; p++ ) {
if ( markedPrime[p] ) {
primes.add((long) p);
Line 517 ⟶ 556:
48 24 12 6 3 142 71 220 110 55
 
1000th member is 990 and highest prime needed is 2273
10000th member is 24975 and highest prime needed is 30713
100000th member is 265781 and highest prime needed is 392111
1000000th member is 8820834 and highest prime needed is 4761697
10000000th member is 41369713 and highest prime needed is 55900829
100000000th member is 1179614168 and highest prime needed is 640692323
 
These numbers under 250 occur the most in the first 1000000000 terms:
Line 1,480 ⟶ 1,519:
 
Extreme stretch not attempted and out of reasonable reach for Wren-CLI.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
Line 1,558 ⟶ 1,597:
 
The following script manages to find the first occurrence of the number 36 in the sequence in about 2 hours 54 minutes which (relative to Phix) is much quicker than one would normally expect. This is probably due to the heavy lifting (i.e. the prime generation) being done here in both cases by C++ though this will tempered by the need to convert unsigned 64-bit integers to doubles (Wren's only numeric type) for each prime generated.
<syntaxhighlight lang="ecmascriptwren">import "./psieve" for Primes
import "./fmt" for Fmt
 
9,476

edits