How to resolve the algorithm Sisyphus sequence step by step in the Perl programming language
How to resolve the algorithm Sisyphus sequence step by step in the Perl programming language
Table of Contents
Problem Statement
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: 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. Find and show on this page (in 10 lines of 10 terms), the first 100 terms of the sequence. What are the 1,000th, 10,000th, 100,000th and 1,000,000th 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. 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:
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sisyphus sequence step by step in the Perl programming language
Source code in the perl programming language
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;
You may also check:How to resolve the algorithm Matrix transposition step by step in the REXX programming language
You may also check:How to resolve the algorithm Make directory path step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Phix programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Python programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the F# programming language