Tom Gordon
Tom Gordon

Reputation: 545

Collatz Conjecture Script Anomaly, Bad Code, or Expected?

I have a simple Collatz Conjecture function that performs the 3n+1 or n/2 on a big (big big) integer value. I was writing every 50k hailstorm numbers to a text file so I could compare numbers without storing the entire 1M plus hailstorm numbers for each test variable. I started to notice that the 50k hailstorm number was the same for almost every number I was trying, even when deviating form the original number by $num - [bigint]::Pow(2, 777) - 1.

I feel like I should expect the numbers to maybe hit the same hailstorm number at some point (1 obviously), but the fact that it always hits at count of 50k seems weird to me. I decided to run it in a loop and to keep subtracting 2^777-1 to see how long until it exits, and it did exit, but then it returned. When it does exit, it only seems to hit 1 number. I'm sure if I kept this going it would find something new at 50K, but this just all seemed odd to me. Am I missing something in the code or is all of this expected Collatz Shenanigan's?

Function Get-CC {
[CmdletBinding()]
    param(
        [Parameter(Mandatory = $false,ValueFromPipeline = $false,ValueFromPipelineByPropertyName = $false,Position = 0)]
        [bigint]  $IntNum,
        [Parameter(Mandatory = $false,ValueFromPipeline = $false,ValueFromPipelineByPropertyName = $false,Position = 1)]
        [bigint]  $TestNum,
        [Parameter(Mandatory = $false,ValueFromPipeline = $false,ValueFromPipelineByPropertyName = $false,Position = 2)]
        [bigint]  $TestPos=50000
    )

    
    $Date = Get-Date -Format "MMddyyyyhhmmss"
    if(!(Test-Path -Path "C:\Collatz\$Date")){
        New-Item -ItemType Directory -Path "C:\Collatz\$Date" -Force | Out-Null
    }
    $count = 0
    $curFile = "C:\Collatz\$Date\cc$($Date)-$($count).txt"
    $IntNum | Out-File -FilePath $curFile

    $write = 50000
    $add = 50000
    While($IntNum -ne 1){
        $count++
        $even = ($IntNum % 2) -eq 0

        if(!$even){
            $IntNum = $IntNum * 3 + 1
          
        }else{
            $IntNum = $IntNum / 2
            
        }

        if($count -eq $TestPos){
            if($TestNum -eq $IntNum){
                Write-Host "Found test number. Exiting sequence..."
                $IntNUm = 1
            }else{
                $TestNum = $IntNum
            }
        }


        if($count -eq $write){
            $lastFile = $curFile
            $curFile = "C:\Collatz\$Date\cc$($Date)-$($count).txt"
            $IntNum | Out-File -FilePath $curFile

            $write = $write + $add
        }
    }



    return @{
        Count = $count
        Num = $IntNum
        TestNum = $TestNum
        }
 
}


$Date = Get-Date -Format "MMddyyyyhhmmss"

$num = [bigint](Get-Content -Path "C:\Collatz\3277.txt")

$TestNum = [bigint](Get-Content -Path "C:\Collatz\09192023030041\cc09192023030041-50000.txt")
for($i=5;$i -lt 100000;$i++){
    $newNum = $num - [bigint]::Pow(2, 777) - 1
    $num = $newNum
    $result = Get-CC -IntNum $num -TestNum $TestNum
    $TestNum = $result.TestNum
    $result
}


The output from this is as follows. So the 50K hailstorm is either 3819... or 9373.... even when deviating from the original number by 2^777-1. It just seems weird to me, but its hard to comprehend numbers this big anyway.

PS C:\Windows\system32> E:\Code\Scripts\Scripts\CC from file 4.ps1
Found test number. Exiting sequence...

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        937344689765502926333681264218296955864719525763636745876570340903072214591257917645664809522167880706050008910390923047278061123345558...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        937344689765502926333681264218296955864719525763636745876570340903072214591257917645664809522167880706050008910390923047278061123345558...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        937344689765502926333681264218296955864719525763636745876570340903072214591257917645664809522167880706050008910390923047278061123345558...
Num                            1                                                                                                                                         
Count                          3830777                                                                                                                                   
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...
Found test number. Exiting sequence...
Num                            1                                                                                                                                         
Count                          50000                                                                                                                                     
TestNum                        381928334828457711200301210511765407693780932193463108339714252575226870306253684835311738919785989232887380000099394237942518019339420...


Upvotes: 0

Views: 29

Answers (0)

Related Questions