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/parsing"
"log"
"strings"
)
func main() {
calculator := collatz.NewCalculator(new(parsing.Cliargs))
err := calculator.CalculateCollatz()
sequence, err := runCollatz()
if err != nil {
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

@ -11,7 +11,11 @@ type Calculator struct {
three *big.Int
two *big.Int
one *big.Int
iterations int
sequence Sequence
}
type Sequence struct {
Iterations int
Sequence []*big.Int
}
func NewCalculator(parser parsing.Parser) *Calculator {
@ -20,40 +24,38 @@ func NewCalculator(parser parsing.Parser) *Calculator {
three: big.NewInt(3),
two: big.NewInt(2),
one: big.NewInt(1),
iterations: 0,
}
return r
}
func (o *Calculator) CalculateCollatz() error {
number, err := o.parser.Parse()
func (calc *Calculator) CalculateCollatz() (Sequence, error) {
number, err := calc.parser.Parse()
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))
err = o.nextNumber(*number)
calc.sequence = Sequence{Iterations: 0}
calc.sequence.Sequence = append(calc.sequence.Sequence, new(big.Int).Set(number))
err = calc.calculateNextCollatzNumber(*number)
if err != nil {
return err
return Sequence{}, err
}
fmt.Println()
fmt.Println("Iterations:", o.iterations)
return nil
return calc.sequence, nil
}
func (o *Calculator) nextNumber(in big.Int) error {
if in.Cmp(o.one) < 0 {
func (calc *Calculator) calculateNextCollatzNumber(in big.Int) error {
if in.Cmp(calc.one) < 0 {
return fmt.Errorf("must be greater than 0")
}
if in.Cmp(o.one) == 0 {
if in.Cmp(calc.one) == 0 {
return nil
}
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)
if new(big.Int).And(&in, calc.one).Cmp(calc.one) == 0 {
newNumber = new(big.Int).Add(new(big.Int).Mul(&in, calc.three), calc.one)
} else {
newNumber = new(big.Int).Div(&in, o.two)
newNumber = new(big.Int).Div(&in, calc.two)
}
o.iterations++
fmt.Printf(" -> %s", newNumber.Text(10))
return o.nextNumber(*newNumber)
calc.sequence.Iterations++
calc.sequence.Sequence = append(calc.sequence.Sequence, newNumber)
return calc.calculateNextCollatzNumber(*newNumber)
}

View File

@ -38,8 +38,11 @@ func TestCalculateCollatz(t *testing.T) {
parser := &MockParser{value: big.NewInt(6)}
calculator := NewCalculator(parser)
err := calculator.CalculateCollatz()
sequence, err := calculator.CalculateCollatz()
if err != nil {
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 {
value *big.Int
}
func (arg *Cliargs) Parse() (*big.Int, error) {
@ -19,10 +18,5 @@ func (arg *Cliargs) Parse() (*big.Int, error) {
if !success {
return nil, errors.New("argument is not an integer")
}
arg.value = num
return num, nil
}
func (arg *Cliargs) GetNumber() *big.Int {
return arg.value
}