Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The "Ulam Conjecture" (Collatz conjecture)
#1
The Ulam conjecture is a popular task in IT teaching. This is an implementation in QB64.

A "problem" arises when the output is very long, because then the heading can no longer be read because the side scroll bars are missing.

Code: (Select All)
' EP in C' S.47, šb.4.6 'Ulamsches Problem': Von einer beliebigen Startzahl
' Erweitert fuer grosse Zahlen (Formatierung noch anpassen)                 -  09.04.2022
' QuickBasic 64 Version                                                     -  23.05.2022
'========================================================================================

OPTION _EXPLICIT

DIM startzahl AS LONG, sum AS LONG
DIM z AS INTEGER

CLS
PRINT TAB(25); "** ULAMSCHES PROBLEM **"
PRINT TAB(25); "======================="
PRINT TAB(10); "Erzeugt eine Folge von Zahlen aus einer gegebenen Startzahl"
PRINT TAB(10); "nach der Regel:"
PRINT TAB(10); "   1. Ist die Zahl 1, dann Stop (Ende)."
PRINT TAB(10); "   2. Ist die Zahl gerade, wird sie halbiert. Gehe nach (1.)"
PRINT TAB(10); "   3. Ist die Zahl ungerade, wird sie verdreifacht und um eins"
PRINT TAB(10); "      vermehrt. Gehe nach (1.)"

PRINT
INPUT "Geben Sie eine (ganze) Startzahl ein: ", startzahl

PRINT "Ergibt die Folge -->"
PRINT

z = 0
sum = 0
WHILE (startzahl <> 1)
  IF startzahl MOD 2 = 0 THEN
    startzahl = startzahl / 2 'Wenn startzahl gerade ist (Regel 2)
  ELSE
    startzahl = startzahl * 3 'Wenn startzahl ungerade ist (Regel 3)
    startzahl = startzahl + 1
  END IF
  PRINT USING "######"; startzahl;
  sum = sum + startzahl
  z = z + 1

  IF z MOD 11 = 0 THEN 'Fuer Formatierung der Ausgabe
    PRINT
  END IF
WEND

PRINT: PRINT
PRINT USING "Diese Folge besteht aus #### Zahlen. Ihre Summe betraegt ######"; z, sum

END


[Image: Normale-Darstellung2022-05-23.jpg]
Reply


Messages In This Thread
The "Ulam Conjecture" (Collatz conjecture) - by Kernelpanic - 05-23-2022, 07:15 PM



Users browsing this thread: 1 Guest(s)