Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
61414ff5bf | ||
713f70041b |
24
collatz.go
24
collatz.go
@ -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
|
||||
}
|
||||
|
@ -7,53 +7,55 @@ import (
|
||||
)
|
||||
|
||||
type Calculator struct {
|
||||
parser parsing.Parser
|
||||
three *big.Int
|
||||
two *big.Int
|
||||
one *big.Int
|
||||
iterations int
|
||||
parser parsing.Parser
|
||||
three *big.Int
|
||||
two *big.Int
|
||||
one *big.Int
|
||||
sequence Sequence
|
||||
}
|
||||
type Sequence struct {
|
||||
Iterations int
|
||||
Sequence []*big.Int
|
||||
}
|
||||
|
||||
func NewCalculator(parser parsing.Parser) *Calculator {
|
||||
r := &Calculator{
|
||||
parser: parser,
|
||||
three: big.NewInt(3),
|
||||
two: big.NewInt(2),
|
||||
one: big.NewInt(1),
|
||||
iterations: 0,
|
||||
parser: parser,
|
||||
three: big.NewInt(3),
|
||||
two: big.NewInt(2),
|
||||
one: big.NewInt(1),
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user