1 people like it.

Collatz Conjecture Tester

A simple test program that prints the number of steps taken when evaluating the Collatz Conjecture. More info here: https://www.youtube.com/watch?v=5mFpVDpKX70

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
open System
open System.Numerics
open System.Globalization

let (|BigInt|_|) str = 
    Some(System.Numerics.BigInteger.Parse(str, NumberStyles.None))

let (|Even|_|) (i: BigInteger) = if i.IsEven then Some Even else None
let (|Odd|_|) (i: BigInteger) = if i.IsEven then None else Some Odd

let collatzify (i: BigInteger) = 
    let rec stepCountingCollatzify step (input: BigInteger) =
        printf "Step %A: %A\n" step input
        match input with
        | _ when input.IsOne -> step
        | Even -> stepCountingCollatzify (step + 1) (input / (bigint 2))
        | Odd -> 
            let newInput = ((bigint 3) * input + (bigint 1))
            printf "Step %A: %A\n" (step + 1) newInput                      // Performs 2 steps at once
            stepCountingCollatzify (step + 2) ( newInput / (bigint 2) )
        | _ -> printf "\nReached %A at step %A in error!" input step; -1
    let steps = stepCountingCollatzify 0 i 
    printf "\nCollatzified  %A to 1 in %i steps." i steps

[<EntryPoint>]
let main args =
    match args.Length with
    | 0  -> printf "Pass a starting value to test the collatz conjecture"
    | _ ->
        match args.[0] with
        | BigInt i -> collatzify i 
        | _ -> printf "Invalid input"
    0
namespace System
namespace System.Numerics
namespace System.Globalization
val str : string
union case Option.Some: Value: 'T -> Option<'T>
Multiple items
type BigInteger =
  struct
    new : value:int -> BigInteger + 7 overloads
    member CompareTo : other:int64 -> int + 3 overloads
    member Equals : obj:obj -> bool + 3 overloads
    member GetHashCode : unit -> int
    member IsEven : bool
    member IsOne : bool
    member IsPowerOfTwo : bool
    member IsZero : bool
    member Sign : int
    member ToByteArray : unit -> byte[]
    ...
  end

Full name: System.Numerics.BigInteger

--------------------
BigInteger()
BigInteger(value: int) : unit
BigInteger(value: uint32) : unit
BigInteger(value: int64) : unit
BigInteger(value: uint64) : unit
BigInteger(value: float32) : unit
BigInteger(value: float) : unit
BigInteger(value: decimal) : unit
BigInteger(value: byte []) : unit
BigInteger.Parse(value: string) : BigInteger
BigInteger.Parse(value: string, provider: IFormatProvider) : BigInteger
BigInteger.Parse(value: string, style: NumberStyles) : BigInteger
BigInteger.Parse(value: string, style: NumberStyles, provider: IFormatProvider) : BigInteger
type NumberStyles =
  | None = 0
  | AllowLeadingWhite = 1
  | AllowTrailingWhite = 2
  | AllowLeadingSign = 4
  | AllowTrailingSign = 8
  | AllowParentheses = 16
  | AllowDecimalPoint = 32
  | AllowThousands = 64
  | AllowExponent = 128
  | AllowCurrencySymbol = 256
  ...

Full name: System.Globalization.NumberStyles
field NumberStyles.None = 0
val i : BigInteger
property BigInteger.IsEven: bool
union case Option.None: Option<'T>
val collatzify : i:BigInteger -> unit

Full name: Script.collatzify
val stepCountingCollatzify : (int -> BigInteger -> int)
val step : int
val input : BigInteger
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
property BigInteger.IsOne: bool
active recognizer Even: BigInteger -> unit option

Full name: Script.( |Even|_| )
type bigint = BigInteger

Full name: Microsoft.FSharp.Core.bigint
active recognizer Odd: BigInteger -> unit option

Full name: Script.( |Odd|_| )
val newInput : BigInteger
val steps : int
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

Full name: Microsoft.FSharp.Core.EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : args:string [] -> int

Full name: Script.main
val args : string []
property Array.Length: int
active recognizer BigInt: string -> BigInteger option

Full name: Script.( |BigInt|_| )
Raw view Test code New version

More information

Link:http://fssnip.net/7QO
Posted:8 years ago
Author:Avnish C. Patel
Tags: #algebra #algebraicmanipulation #algebraic , mathematics , numberphile