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
#2
A correction to intercept by 0 or letters.  Rolleyes


Quote:. . .
PRINT
DO
  INPUT "Geben Sie eine (ganze) Startzahl ein: ", startzahl
LOOP WHILE startzahl = 0 OR startzahl <> INT(startzahl)
PRINT "Ergibt die Folge -->"
. . .
Reply
#3
This is my program for this problem:

Code: (Select All)
'
' ***************************
' *  PROGRAMM: COLLATZ.bas  *
' ***************************
'
' geschrieben: 23.08.2017, BSpinoza
' bearbeitet: 20.05.2020
'
' PROGRAM Collatz Conjecture
' ==============================================================
' The Collatz Conjecture states that if you pick a number n
' and if n is even, divide it by 2 to get n/2,
' If n is odd multiply it by 3 and add 1 to obtain 3n+1.
' Repeat this process indefinitely until it becomes 1.
' The conjecture is that no matter what number you start with,
' you will always eventually reach 1.
' Paul Erdos said about the Collatz conjecture:
' "Mathematics is not yet ready for such problems."
' He offered $500 USD FOR its solution.
' Thwaites (1996) has offered a $1000 reward for resolving
' the conjecture.
'
' =============================================================
'
' Die Collatz-Vermutung ist eine Vermutung über Zahlenfolgen,
' die 1937 von dem Mathematiker Lothar Collatz geäußert wurde.
' Er konstruierte diese Zahlenfolgen nach einem einfachen
' Bildungsgesetz:
'
' 1. Man denke sich eine natürliche Zahl
' 2. Falls die Zahl gerade ist, teilt man sie durch 2.
' 3. Falls die Zahl ungerade ist, multipliziert man sie mit 3
'    und addiert 1.
'
' Beispiel:
' Man nehme z. B. die 11. Nach obigem Bildungsgesetz ergibt
' sich folgende Zahlenfolge:
' 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ...
' Die Collatz-Vermutung lautet:
' Jede so konstruierte Zahlenfolge mündet in den Zyklus 4, 2, 1,
' egal, mit welcher natürlichen Zahl man beginnt.
' Die Mathematiker haben bisher keine Gegenbeispiele gefunden.
' ==============================================================
'
Dim cnt As _Unsigned _Integer64, n As _Unsigned _Integer64
cnt = 0
Input "Input of an Integer: ", n
While (n <> 1)
    If n Mod 2 = 0 Then
        n = n / 2
        Print "   ->  even"
    Else
        n = 3 * n + 1
        Print "   ->  odd"
    End If
    cnt = cnt + 1
    Print n,
Wend
Print
Print " >>> The Collatz function is applied for "; cnt; " times."
End
Reply
#4
One thing that makes this problem hard is - what would a proof of the conjecture actually look like?

One thing you might do is try to disprove the existence of "cycles", i.e. sequences that enter an infinite loop. One such cycle exists, and without much context, here is a paste of the proof that it's the *only* cycle of its kind:


[Image: aaaaaaaaaaaaa.png]
Reply
#5
For those interested in mathematics (I'm not a mathematician), if you don't already know:

Collatz conjecture - Wikipedia
Reply
#6
No one here takes anyone with a Looney Tunes avatar seriously! Oops, never mind...

Pete
If eggs are brain food, Biden has his scrambled.

Reply
#7
Code: (Select All)
_Title "Collatz collapse #2" 'b+ 2022-05-24
_Define A-Z As _INTEGER64
Width , 48
For start = 1 To 2298024 ' to just before the screen scrolls
    steps = 0: n = start: max = 0
    While n <> 1
        If n Mod 2 Then
            n = n * 3 + 1
            If n > max Then max = n
            steps = steps + 1
        End If
        While n Mod 2 = 0
            n = n / 2
            steps = steps + 1
        Wend
    Wend
    If n = 1 Then 'Print start; "took"; steps; "steps."
        If steps > maxsteps Then Print start; "took"; steps; "steps, high"; max: maxsteps = steps
    End If
Next

Collatz collapse revisited.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)