Sisyphus sequence

From Rosetta Code
Revision as of 11:36, 8 June 2023 by PureFox (talk | contribs) (Created a new draft task and added a Wren example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sisyphus sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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,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?

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


Wren

Library: Wren-math
Library: Wren-fmt

No option here but to use a sieve as relying on the 'nextPrime' method would be far too slow to achieve the stretch goal in a reasonable time using Wren. Sieve limit found by experimentation.

Extreme stretch not attempted and probably out of the question for Wren.

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
}
Output:
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 and highest prime needed:       2,273
     10,000th member is:        24,975 and highest prime needed:      30,713
    100,000th member is:       265,781 and highest prime needed:     392,111
  1,000,000th member is:     8,820,834 and highest prime needed:   4,761,697
 10,000,000th member is:    41,369,713 and highest prime needed:  55,900,829
100,000,000th member is: 1,179,614,168 and highest prime needed: 640,692,323

These numbers under 250 do not occur in the first 100,000,000 terms:
  [36, 72, 97, 107, 115, 127, 144, 167, 194, 211, 214, 230, 232]

These numbers under 250 occur the most in the first 100,000,000 terms:
  [7, 14, 28] all occur 7 times.