$count = 1
do {
$count++
$i = $count
[string]$array = "$i"
$range = $i - 1
do {
if ($i % 2 -eq 0) {$i = $i / 2} else {$i = (3 \* $i) + 1}
$array = "$array" + ",$i"
if ($i - $count -gt $range) {$range = $i - $count}
if ($i -eq 2) {$i = "Break"}
} while ($i -ne "Break")
$array = "$array" + ",1"
$hits = (($array -split ",") | Measure-Object).count
Set-Content -Path "${count}_${hits}Hits_${range}MaxRange.txt" -Value "$array" -NoNewline -Force
} while ($count -lt 1000)
Does anyone know how to make this work with big numbers? At a certain point the value gets returned with something like 2.05891132094649E+44 at which point I can no longer simply add 1 to it.Edit: Found it... $count = [bigint][math]::pow(10,44) Awesome! I love code!
So my attempt to find a counterexample is to start at (2^68)+1, and perform the 3x+1 or halving until I get to a number that's lower than the one I'm testing - then I know it's not a counterexample.
Since even numbers start by halving (ie, getting lower), I only test odd numbers.
295147905503560000001 and counting. No counter-examples found yet.
using System.Numerics;
using System;
var myBigStartingNumber = BigInteger.Parse("12893123812148934789012378957891325789012357891238912319824589123589012358915891589158989125");
Collatz(myBigStartingNumber);
Console.WriteLine("Collatz returned 1");
static int Collatz(BigInteger x)
{
Console.WriteLine(x);
return x == 1 ? 1 : x % 2 == 0 ? Collatz(x / 2) : Collatz(3 * x + 1);
}
(stack overflows virtually guaranteed!)But something which is worth doing while playing with such programs, (besides writing it nonrecursively), is looking into alternative BigNum representations. Tree based number representations can make huge numbers expressible through few operations much smaller, at the cost of making "typical" numbers (those that can't be expressed by arithmetic expressions much shorter than themselves) only slightly larger.
Knuth made one such representation, called TCALC, which lets you do arithmetic on numbers far too large to fit into computer memory in regular byte string bignum representation. A US academic, Paul Tarau, has made similar huge-num libraries (slightly more elegant since representations are unique) for modern programming languages.
Overall, there might be a connection to complex systems and ontogenesis. Maybe our struggling with this is all down to some original sin in our perception. Some false axiom we all carry. Pathogenic, hereditary logic, which prevents us from seeing beyond the chaos. 3x+1 seems to capture the pleasing ordered imperfection of real living things, not artificial "organic" structures made by humans.
A/(B+C) + B/(A+C) + C/(A+B) = 4
It seems simple, like you could try a few examples and figure it out. And there is a solution. But for the smallest solution, A, B, and C each have 80 digits. Way too big to brute force.
I didn’t think that a bit of coding was going to outpace hundreds of years of genius mathematicians - but a few minutes coding was a cheap price to pay to satisfy that ceaseless curiosity
Did this in Haskell, and since all the lower numbers are known, started searching at 2^361
To end the recursion I just have it print -1 when it reaches 1
f :: Integer -> Integer
f n
| n == 1 = -1
| even n = f (n `div` 2)
| odd n = f (3*n + 1)
main = print $ map f [2^361..]