Sisyphus sequence: Difference between revisions

Added Perl
(→‎{{header|Python}}: Extreme stretch)
(Added Perl)
Line 196:
Integers in 1..250 that occur most often ( 6 times ) up to element 10000000:
3 57 65 85 114 125 130 170 228
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>
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;
</syntaxhighlight>
{{out}}
<pre>
The first 100 members of the Sisyphus sequence are:
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
 
1,000th member is: 990 with prime: 2,273
10,000th member is: 24,975 with prime: 30,713
100,000th member is: 265,781 with prime: 392,111
1,000,000th member is: 8,820,834 with prime: 4,761,697
10,000,000th member is: 41,369,713 with prime: 55,900,829
100,000,000th member is: 1,179,614,168 with prime: 640,692,323
 
Numbers under 250 that do not occur in the first 100,000,000 terms:
36 72 97 107 115 127 144 167 194 211 214 230 232
 
Numbers under 250 occur the most (7 times) in the first 100,000,000 terms:
7 14 28
</pre>
 
2,392

edits