Compare commits

..

2 Commits
v0.2 ... main

Author SHA1 Message Date
Logiar
61414ff5bf Write the chain on separate lines.
All checks were successful
Run Tests / test (push) Successful in 31s
release / release (push) Successful in 29s
2024-09-11 05:33:20 +00:00
713f70041b
Clean code improvements.
All checks were successful
Run Tests / test (push) Successful in 9s
2024-04-25 04:41:39 +02:00
4 changed files with 56 additions and 37 deletions

View File

@ -4,12 +4,32 @@ import (
"collatz/collatz" "collatz/collatz"
"collatz/parsing" "collatz/parsing"
"log" "log"
"strings"
) )
func main() { func main() {
calculator := collatz.NewCalculator(new(parsing.Cliargs)) sequence, err := runCollatz()
err := calculator.CalculateCollatz()
if err != nil { if err != nil {
log.Fatalf("Error calculating Collatz: %v", err) log.Fatalf("Error calculating Collatz: %v", err)
} }
printSequence(sequence)
}
func printSequence(sequence collatz.Sequence) {
builder := new(strings.Builder)
log.Printf("Collatz sequence with %d iterations:", sequence.Iterations)
for i, num := range sequence.Sequence {
if i != 0 {
builder.WriteString("\n-> ")
}
builder.WriteString(num.String())
}
log.Println(builder.String())
}
func runCollatz() (collatz.Sequence, error) {
parser := new(parsing.Cliargs)
calculator := collatz.NewCalculator(parser)
sequence, err := calculator.CalculateCollatz()
return sequence, err
} }

View File

@ -7,53 +7,55 @@ import (
) )
type Calculator struct { type Calculator struct {
parser parsing.Parser parser parsing.Parser
three *big.Int three *big.Int
two *big.Int two *big.Int
one *big.Int one *big.Int
iterations int sequence Sequence
}
type Sequence struct {
Iterations int
Sequence []*big.Int
} }
func NewCalculator(parser parsing.Parser) *Calculator { func NewCalculator(parser parsing.Parser) *Calculator {
r := &Calculator{ r := &Calculator{
parser: parser, parser: parser,
three: big.NewInt(3), three: big.NewInt(3),
two: big.NewInt(2), two: big.NewInt(2),
one: big.NewInt(1), one: big.NewInt(1),
iterations: 0,
} }
return r return r
} }
func (o *Calculator) CalculateCollatz() error { func (calc *Calculator) CalculateCollatz() (Sequence, error) {
number, err := o.parser.Parse() number, err := calc.parser.Parse()
if err != nil { if err != nil {
return fmt.Errorf("couldn't parse input: %w", err) return Sequence{}, fmt.Errorf("couldn't parse input: %w", err)
} }
fmt.Print(number.Text(10)) calc.sequence = Sequence{Iterations: 0}
err = o.nextNumber(*number) calc.sequence.Sequence = append(calc.sequence.Sequence, new(big.Int).Set(number))
err = calc.calculateNextCollatzNumber(*number)
if err != nil { if err != nil {
return err return Sequence{}, err
} }
fmt.Println() return calc.sequence, nil
fmt.Println("Iterations:", o.iterations)
return nil
} }
func (o *Calculator) nextNumber(in big.Int) error { func (calc *Calculator) calculateNextCollatzNumber(in big.Int) error {
if in.Cmp(o.one) < 0 { if in.Cmp(calc.one) < 0 {
return fmt.Errorf("must be greater than 0") return fmt.Errorf("must be greater than 0")
} }
if in.Cmp(o.one) == 0 { if in.Cmp(calc.one) == 0 {
return nil return nil
} }
var newNumber *big.Int var newNumber *big.Int
if new(big.Int).And(&in, o.one).Cmp(o.one) == 0 { if new(big.Int).And(&in, calc.one).Cmp(calc.one) == 0 {
newNumber = new(big.Int).Add(new(big.Int).Mul(&in, o.three), o.one) newNumber = new(big.Int).Add(new(big.Int).Mul(&in, calc.three), calc.one)
} else { } else {
newNumber = new(big.Int).Div(&in, o.two) newNumber = new(big.Int).Div(&in, calc.two)
} }
o.iterations++ calc.sequence.Iterations++
fmt.Printf(" -> %s", newNumber.Text(10)) calc.sequence.Sequence = append(calc.sequence.Sequence, newNumber)
return o.nextNumber(*newNumber) return calc.calculateNextCollatzNumber(*newNumber)
} }

View File

@ -38,8 +38,11 @@ func TestCalculateCollatz(t *testing.T) {
parser := &MockParser{value: big.NewInt(6)} parser := &MockParser{value: big.NewInt(6)}
calculator := NewCalculator(parser) calculator := NewCalculator(parser)
err := calculator.CalculateCollatz() sequence, err := calculator.CalculateCollatz()
if err != nil { if err != nil {
t.Errorf("Expected no error, got %v", err) t.Errorf("Expected no error, got %v", err)
} }
if sequence.Iterations != 8 {
t.Errorf("Expected 8 iterations, got %v", sequence.Iterations)
}
} }

View File

@ -7,7 +7,6 @@ import (
) )
type Cliargs struct { type Cliargs struct {
value *big.Int
} }
func (arg *Cliargs) Parse() (*big.Int, error) { func (arg *Cliargs) Parse() (*big.Int, error) {
@ -19,10 +18,5 @@ func (arg *Cliargs) Parse() (*big.Int, error) {
if !success { if !success {
return nil, errors.New("argument is not an integer") return nil, errors.New("argument is not an integer")
} }
arg.value = num
return num, nil return num, nil
} }
func (arg *Cliargs) GetNumber() *big.Int {
return arg.value
}