Initial commit
This commit is contained in:
		
						commit
						9ed8f02013
					
				
							
								
								
									
										11
									
								
								collatz.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								collatz.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										54
									
								
								collatz/calculator.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								io/cliargs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								io/cliargs.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user