Jump to content

Collatz Conjecture


layer
 Share

Recommended Posts

so literally just tonight, i realized and identified the fact that ive always been amazed at math theories, and unsolved math problems.. i always was, but just never really knew it, but at the same time i did :geek:

well since i havent been using autoit a lot latley or any programming language, just because, it took me a while to find out that i needed $check inside the loop and that the script wasnt functioning like it should because i did $snum/3+1 when it was supposed to be $snum*3+1.. that mistake was because i was tired :"> (check code below to know what im talking about)

so basically, the Collatz Conjecture (theory;guess) is a theory thought up Lothar Collatz, stating that if you multiply every odd number by three and add one to it, and divide ever even number by two, sooner or later (it may take a long while, but sooner or later) you will get to the "never ending" 4, 2, 1 ... so this code just lets you input a starting number, and a messagebox that you have to click ok to keep the loop going will pop up after every calculation..

for more info on the Collatz Conjecture, http://en.wikipedia.org/wiki/Collatz_conjecture , and you can search google

oh, and one more thing ! try starting out w/ a number like 20 or something, and in about 5 numbers, you will get the 4, 2, 1 .. if you find a number that doesnt produce 4, 2, 1 when it gets to that point (like when you get down the the single digits, who knows how long that will take with some numbers, id imagine well over a hundred, give or take a few hundred, well, dont tell anyone except someone who will pay you for the number, because you just solved the Collatz conjecture

#include <math.au3>
#include <GUIConstants.au3>

$snum = Number(InputBox("Enter starting number", "Enter the number you want to start the Collatz conjecture"))
If $snum = "" Then Exit

GUICreate("Collatz Conjecture", 350, 450)
$edit = GUICtrlCreateEdit("", 10, 50, 300, 300, BitOR($WS_DISABLED, $WS_VSCROLL))
$curnum = GUICtrlCreateEdit("", 10, 375, 100, 25, $ES_READONLY)
$snumlabel = GUICtrlCreateLabel("Starting number: " & $snum, 10, 10)
$file = GUICtrlCreateMenu("File")
;$file1 = GUICtrlCreateMenuItem("Open", $file)
$file2 = GUICtrlCreateMenuitem("Start with new number", $file)
$file3 = GUICtrlCreateMenuitem("Save", $file)
GUICtrlCreateMenuitem("", $file)
$file4 = GUICtrlCreateMenuitem("Exit", $file)

GUISetState()

Func MainLoop()
   GUICtrlSetData($edit, $snum & @CRLF)
   While 1

      $get = GUIGetMsg()
      If $get = -3 Then Exit

      Sleep(50)

      GUICtrlSetData($curnum, $snum, "")


      $check = _MathCheckDiv($snum, 2)

      If $check = -1 Or @error = 1 Then
         MsgBox(0, "Error", "You entered an invalid number, exiting.")
         Exit
      ElseIf $check = 1 Then
         $snum = $snum * 3 + 1
         GUICtrlSetData($edit, $snum & @CRLF, 1)
        ;MsgBox(0, "1", $snum)
      ElseIf $check = 2 Then
         $snum = $snum / 2
         GUICtrlSetData($edit, $snum & @CRLF, 1)
        ;MsgBox(0, "2", $snum)
      Else
         MsgBox(0, "Error", "Error parsing, exiting.")
         Exit
      EndIf

      $CHECKNUMS = Number(GUICtrlRead($curnum))
      If $CHECKNUMS = 4 Then
         $continue = MsgBox(4, "Complete", "The number 4 has been reached, 4, 2, 1 will be repeated, keep going ?")
         If $continue = 6 Then
            ContinueLoop
         ElseIf $continue = 7 Then
            GUICtrlSetStyle($edit, BitOR($ES_READONLY, $WS_VSCROLL))
            ExitLoop
         EndIf
      EndIf

   WEnd
EndFunc  ;==>MainLoop
MainLoop()

While 1
   $get = GUIGetMsg()
   If $get = -3 Then Exit
   If $get = $file2 Then
      GUICtrlSetData($edit, "", "")
      $snum = Number(InputBox("Enter starting number", "Enter the number you want to start the Collatz conjecture"))
      If $snum = "" Then Exit
      GUICtrlSetData($snumlabel, "Starting number: " & $snum, "")
      GUICtrlSetStyle($edit, BitOR($WS_DISABLED, $WS_VSCROLL))
      MainLoop()
   EndIf
   If $get = $file3 Then
      $path = FileSaveDialog("Save file...", @DesktopDir, "Text (.txt)", 2 + 16, "Collatz Conjecture Log.txt")
      $filehwnd = FileOpen($path, 2)
      FileWrite($path, GUICtrlRead($edit))
   EndIf
   If $get = $file4 Then Exit
WEnd

now that im reading wikipedia, they have pseudocode as well.. but i like to see what the code is doing so i added messageboxes (may take forever if you enter a BIG number :ph34r: )

i would say enjoy, but i know not everyone likes math, well i dont, i like the theory, but not the stuff they teach in school (although some of it from school i like and actually relaxes me and takes some stress away :lmao: )

edit: updated code

edit2: added two lines of code under the inputboxes to check for "" and if so, exit

Edited by layer
FootbaG
Link to comment
Share on other sites

  • Moderators

Try this if you want to watch it.

#include <math.au3>

HotKeySet("{Esc}", "_Exit")

$iNumber = 1

While 1
    $i = $iNumber
    ConsoleWrite($i & " {")
    While $i > 1
        Sleep(1)
        $fCheck = _MathCheckDiv($i, 2)
        If $fCheck = 1 Then
            $i = ($i * 3) + 1
        ElseIf $fCheck = 2 Then
            $i = ($i / 2)
        EndIf
        If $i = 1 Then
            ConsoleWrite($i)
        Else
            ConsoleWrite($i & ", ")
        EndIf
    WEnd
    ConsoleWrite("}" & @CR & @CR)
    $iNumber += 1
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

Try this if you want to watch it.

#include <math.au3>

HotKeySet("{Esc}", "_Exit")

$iNumber = 1

While 1
    $i = $iNumber
    ConsoleWrite($i & " {")
    While $i > 1
        Sleep(1)
        $fCheck = _MathCheckDiv($i, 2)
        If $fCheck = 1 Then
            $i = ($i * 3) + 1
        ElseIf $fCheck = 2 Then
            $i = ($i / 2)
        EndIf
        If $i = 1 Then
            ConsoleWrite($i)
        Else
            ConsoleWrite($i & ", ")
        EndIf
    WEnd
    ConsoleWrite("}" & @CR & @CR)
    $iNumber += 1
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Cool, now, how can a graph of the number of iterations to complete vs initial number be presented?
Link to comment
Share on other sites

check my first post for edited & improved code

i just dont like ConsoleWrite :lmao: so i used GUI, and added a few other little things

edit: oh yea, and using a bigger number doesnt necessarily take longer, try using 27, that takes a lot longer than a number like 1000

this is why i think there is no one number (whole positive number) that will ever NOT yield 4, 2, 1 with this theory, because it all breaks down to odd and evens, and sometimes the size of the number

edit2: well i guess using a number like 1000000000000001 or whatever, is just too big for, well im not sure, but everything goes bleh .. oh well, im sure on paper, it would all work out

Edited by layer
FootbaG
Link to comment
Share on other sites

check my first post for edited & improved code

i just dont like ConsoleWrite :lmao: so i used GUI, and added a few other little things

edit: oh yea, and using a bigger number doesnt necessarily take longer, try using 27, that takes a lot longer than a number like 1000

this is why i think there is no one number (whole positive number) that will ever NOT yield 4, 2, 1 with this theory, because it all breaks down to odd and evens, and sometimes the size of the number

edit2: well i guess using a number like 1000000000000001 or whatever, is just too big for, well im not sure, but everything goes bleh .. oh well, im sure on paper, it would all work out

I think this is why a graph of the iterations would be useful. If you could look at the graph and identify a pattern to the number of iterations required per number, it might help lead to yet more interesting number patterns that might maybe perhaps lead to insight?

Or it might just look cool! B-)

I've been able to modify the script posted by Smoke_N to display the number of iterations each number requires to reach 1 but I have no idea if or how AutoIt could graph it. Maybe write to Excel COM would be the simplest? Then let Excel do all the work.

Looking at the iterations it doesn't look random. Not all over the place random anyway. There seems to be certain numbers of iterations that occur frequently, often in runs...

I dunno. More obvious with graphs anyway ... B-)

Link to comment
Share on other sites

I think this is why a graph of the iterations would be useful. If you could look at the graph and identify a pattern to the number of iterations required per number, it might help lead to yet more interesting number patterns that might maybe perhaps lead to insight?

Or it might just look cool! B-)

I've been able to modify the script posted by Smoke_N to display the number of iterations each number requires to reach 1 but I have no idea if or how AutoIt could graph it. Maybe write to Excel COM would be the simplest? Then let Excel do all the work.

Looking at the iterations it doesn't look random. Not all over the place random anyway. There seems to be certain numbers of iterations that occur frequently, often in runs...

I dunno. More obvious with graphs anyway ... B-)

yea i see what you're saying, i just don't know how to make graphs w/ autoit :">
FootbaG
Link to comment
Share on other sites

I also have experimented around with this.

This script produces a file on the desktop with the number, the iterations, then the numbers presented in columns

#region --- GuiBuilder code Start ---
; Script generated by AutoBuilder 0.6 Prototype

#include <GuiConstants.au3>
#include <file.au3>

GuiCreate("MyGUI", 388, 322,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$List_1 = GuiCtrlCreateedit("", 20, 10, 350, 266)
$Button_2 = GuiCtrlCreateButton("Start Collatz Calc.", 20, 290, 180, 20)
$Input_3 = GuiCtrlCreateInput("", 220, 290, 150, 20)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button_2=$msg
        start()
        msgbox(0,"Completed","Calculations Completed.")
    EndSelect
WEnd
Exit
#endregion --- GuiBuilder generated code End ---

func start()
    progresson("Collatz Calculations","")
    
    _filecreate(@DesktopCommonDir & "\Collatz.txt")
    guictrlsetdata($List_1,"")
    $first = stringformat("%i",guictrlread($Input_3))
    $original = $first
    for $var = 1 to $original
        progressset($var/$original*100,"Creating File")
        $first = $var
        $iterations = 1
        guictrlsetdata($List_1,"")
        while 1
            sleep(10)
            guictrlsetdata($List_1,guictrlread($List_1) & @crlf& $first)
            if $first = 1 then ExitLoop
            if $first/2 = int($first/2) then 
                $second = $first/2
            Else
                $second = $first*3 +1   
            EndIf
            $first = $second
            $iterations = $iterations + 1
        WEnd
        FileWriteLine(@DesktopCommonDir & "\Collatz.txt",$var & @TAB &$iterations &stringreplace(guictrlread($List_1),@crlf,@tab))
        guictrlsetdata($List_1,guictrlread($List_1) & @crlf& @crlf & "Iterations:" & $iterations)
    next
    progressoff()
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...