Collatz 猜想脚本异常、代码错误还是意料之中?

Collatz Conjecture Script Anomaly, Bad Code, or Expected?

提问人:Tom Gordon 提问时间:9/20/2023 更新时间:9/20/2023 访问量:14

问:

我有一个简单的 Collatz 猜想函数,它对一个大(大)整数值执行 3n+1 或 n/2。我将每个 50k 的冰雹数字写入一个文本文件,这样我就可以比较数字,而无需存储每个测试变量的整个 1M 加上冰雹数字。我开始注意到,对于我尝试的几乎每个数字,50k 冰雹数字都是相同的,即使偏离原始数字 $num - [bigint]::P ow(2, 777) - 1。

我觉得我应该期望这些数字可能会在某个时候达到相同的冰雹数字(显然是 1),但它总是以 50k 的计数击中这一事实对我来说似乎很奇怪。我决定在循环中运行它,并继续减去 2^777-1,看看它退出了多长时间,它确实退出了,但后来它又回来了。当它退出时,它似乎只击中了 1 个数字。我敢肯定,如果我继续下去,它会在 50K 时找到新的东西,但这对我来说似乎很奇怪。我是在代码中遗漏了什么,还是所有这些都是 Collatz 恶作剧的?

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
}


其输出如下。所以 50K 的冰雹要么是 3819......或 9373....即使偏离原始数字 2^777-1。这对我来说似乎很奇怪,但无论如何都很难理解这么大的数字。

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...


PowerShell Collatz

评论

0赞 Tom Gordon 9/20/2023
这是我上次使用的数字,但我测试了其他数字,结果相同。drive.google.com/file/d/1blvyGVWkXMpSuPsIw1BQuGE08SPrRn4y/......
0赞 Tom Gordon 9/20/2023
我让它继续运行,它最终以第三个 50k 号码退出,以 8982 开头,但总停止时间仍然完全相同,为 3,830,777。我在想我的代码必须有一些东西。当偏离 2^777 时,我应该得到不同的停止时间,对吧?有点巧合,我的偏差是 2^777-1,总停止时间也总是以 777 结束。

答: 暂无答案