Initial commit

This commit is contained in:
Lars Mikal Rogne 2024-04-24 19:49:03 +02:00
commit 9ed8f02013
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
4 changed files with 96 additions and 0 deletions

11
collatz.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"collatz/collatz"
"collatz/io"
)
func main() {
calculator := collatz.NewCalculator(new(io.Cliargs))
calculator.Collatz()
}

54
collatz/calculator.go Normal file
View File

@ -0,0 +1,54 @@
package collatz
import (
"collatz/io"
"fmt"
"log"
"math/big"
)
type Calculator struct {
parser *io.Cliargs
three *big.Int
two *big.Int
one *big.Int
}
func NewCalculator(parser *io.Cliargs) Calculator {
r := Calculator{
parser: parser,
three: new(big.Int).SetInt64(3),
two: new(big.Int).SetInt64(2),
one: new(big.Int).SetInt64(1),
}
return r
}
func (o Calculator) Collatz() {
err := o.parser.Parse()
if err != nil {
log.Panicf("Couldn't parse input: %s\n", err)
}
number := o.parser.GetNumber()
fmt.Print(number.Text(10))
o.nextNumber(number)
fmt.Println()
}
func (o Calculator) nextNumber(in *big.Int) {
if in.Cmp(o.one) < 0 {
log.Panicln("Must be greater than 0")
}
if in.Cmp(o.one) == 0 {
return
}
var newNumber *big.Int
if new(big.Int).And(in, o.one).Cmp(o.one) == 0 {
newNumber = new(big.Int).Add(new(big.Int).Mul(in, o.three), o.one)
} else {
newNumber = new(big.Int).Div(in, o.two)
}
fmt.Printf(" -> %s", newNumber.Text(10))
o.nextNumber(newNumber)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module collatz
go 1.22

28
io/cliargs.go Normal file
View File

@ -0,0 +1,28 @@
package io
import (
"errors"
"math/big"
"os"
)
type Cliargs struct {
value *big.Int
}
func (arg *Cliargs) Parse() error {
args := os.Args[1:]
if len(args) != 1 {
return errors.New("only expected 1 arg")
}
num, success := new(big.Int).SetString(args[0], 10)
if !success {
return errors.New("argument is not an integer")
}
arg.value = num
return nil
}
func (arg *Cliargs) GetNumber() *big.Int {
return arg.value
}