Skip to content

Instantly share code, notes, and snippets.

@wipiano
Last active October 19, 2020 14:40
Show Gist options
  • Save wipiano/2beccb0b54b44304ec85eb4a0121dfb9 to your computer and use it in GitHub Desktop.
Save wipiano/2beccb0b54b44304ec85eb4a0121dfb9 to your computer and use it in GitHub Desktop.
コラッツ予想
function f(n) {
n = BigInt(n)
console.log(n)
while (n != 1) {
if (n % 2n == 0) {
n = n / 2n
} else {
n = n * 3n + 1n
}
console.log("=> " + n)
}
}
using System;
using System.Numerics;
namespace Collatz
{
class Program
{
static void Main(string[] args)
{
var n = new BigInteger(2);
while (true)
{
RunOnce(n);
n++;
}
}
static void RunOnce(BigInteger n)
{
Console.Write(n.ToString());
while (n != 1)
{
var div2 = BigInteger.DivRem(n, 2, out var remainder);
n = remainder == BigInteger.Zero ? div2 : n * 3 + 1;
Console.Write($" => {n}");
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment