How to resolve the algorithm Hailstone sequence step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hailstone sequence step by step in the Oberon-2 programming language

Table of Contents

Problem Statement

The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:

The (unproven) Collatz conjecture is that the hailstone sequence for any starting number always terminates.

This sequence was named by Lothar Collatz in 1937   (or possibly in 1939),   and is also known as (the):

The hailstone sequence is also known as   hailstone numbers   (because the values are usually subject to multiple descents and ascents like hailstones in a cloud).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hailstone sequence step by step in the Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE hailst;

IMPORT  Out;

CONST   maxCard         = MAX (INTEGER) DIV 3;
        List            = 1;
        Count           = 2;
        Max             = 3;

VAR     a               : INTEGER;

PROCEDURE HailStone (start, type  : INTEGER) : INTEGER;

VAR     n, max, count           : INTEGER;

BEGIN
  count := 1;
  n := start;
  max := n;
  LOOP
    IF  type = List  THEN
      Out.Int (n, 12);
      IF  count MOD 6 = 0  THEN  Out.Ln  END
    END;
    IF  n = 1  THEN  EXIT  END;
    IF  ODD (n)  THEN
      IF  n < maxCard  THEN
        n := 3 * n + 1;
        IF   n > max  THEN  max := n  END
      ELSE
        Out.String ("Exceeding max value for type INTEGER at: ");
        Out.String (" n = ");           Out.Int (start, 12);
        Out.String (" , count = ");     Out.Int (count, 12);
        Out.String (" and intermediate value ");
        Out.Int (n, 1);
        Out.String (". Aborting.");
        Out.Ln;
        HALT (2)
      END
    ELSE
      n := n DIV 2
    END;
    INC (count)
  END;
  IF  type = Max  THEN  RETURN  max  ELSE  RETURN  count  END
END HailStone;


PROCEDURE FindMax (num   : INTEGER);

VAR     val, maxCount, maxVal, cnt      : INTEGER;

BEGIN
  maxCount := 0;
  maxVal := 0;
  FOR  val := 2 TO num  DO
   cnt := HailStone (val, Count);
    IF  cnt > maxCount  THEN
      maxVal := val;
      maxCount := cnt
    END
  END;
  Out.String ("Longest sequence below ");       Out.Int (num, 1);
  Out.String (" is ");                          Out.Int (HailStone (maxVal, Count), 1);
  Out.String (" for n = ");                     Out.Int (maxVal, 1);
  Out.String (" with an intermediate maximum of ");
  Out.Int (HailStone (maxVal, Max), 1);
  Out.Ln
END FindMax;

BEGIN
  a := HailStone (27, List);
  Out.Ln;
  Out.String ("Iterations total = ");   Out.Int (HailStone (27, Count), 12);
  Out.String (" max value = ");         Out.Int (HailStone (27, Max)  , 12);
  Out.Ln;
  FindMax (1000000);
  Out.String ("Done.");
  Out.Ln
END hailst.

  

You may also check:How to resolve the algorithm Take notes on the command line step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm MD4 step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Euler method step by step in the RPL programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Raku programming language