Compare commits

..

No commits in common. "main" and "v0.1" have entirely different histories.
main ... v0.1

10 changed files with 40 additions and 189 deletions

View File

@ -17,9 +17,9 @@ jobs:
with:
go-version: '>=1.22.1'
- name: release-build-windows
run: GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o bin/collatz-${{ github.ref_name }}-windows-amd64.exe
run: GOOS=windows GOARCH=amd64 go build -o bin/collatz-${{ github.ref_name }}-windows-amd64.exe
- name: release-build-linux
run: GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/collatz-${{ github.ref_name }}-linux-amd64
run: GOOS=linux GOARCH=amd64 go build -o bin/collatz-${{ github.ref_name }}-linux-amd64
- name: Release
uses: https://gitea.com/actions/release-action@main
with:

View File

@ -1,22 +0,0 @@
name: Run Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.22.1'
- name: Run tests
run: go test ./...

1
.gitignore vendored
View File

@ -1 +0,0 @@
.idea

View File

@ -1 +0,0 @@
Run with `go run collatz.go 1234` or download the executable and pass a number `collatz 1234`.

View File

@ -2,34 +2,10 @@ package main
import (
"collatz/collatz"
"collatz/parsing"
"log"
"strings"
"collatz/io"
)
func main() {
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
calculator := collatz.NewCalculator(new(io.Cliargs))
calculator.Collatz()
}

View File

@ -1,61 +1,53 @@
package collatz
import (
"collatz/parsing"
"collatz/io"
"fmt"
"log"
"math/big"
)
type Calculator struct {
parser parsing.Parser
parser *io.Cliargs
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{
func NewCalculator(parser *io.Cliargs) Calculator {
r := Calculator{
parser: parser,
three: big.NewInt(3),
two: big.NewInt(2),
one: big.NewInt(1),
three: new(big.Int).SetInt64(3),
two: new(big.Int).SetInt64(2),
one: new(big.Int).SetInt64(1),
}
return r
}
func (calc *Calculator) CalculateCollatz() (Sequence, error) {
number, err := calc.parser.Parse()
func (o Calculator) Collatz() {
number, err := o.parser.Parse()
if err != nil {
return Sequence{}, fmt.Errorf("couldn't parse input: %w", err)
log.Panicf("Couldn't parse input: %s\n", err)
}
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 Sequence{}, err
}
return calc.sequence, nil
fmt.Print(number.Text(10))
o.nextNumber(number)
fmt.Println()
}
func (calc *Calculator) calculateNextCollatzNumber(in big.Int) error {
if in.Cmp(calc.one) < 0 {
return fmt.Errorf("must be greater than 0")
func (o Calculator) nextNumber(in *big.Int) {
if in.Cmp(o.one) < 0 {
log.Panicln("Must be greater than 0")
}
if in.Cmp(calc.one) == 0 {
return nil
if in.Cmp(o.one) == 0 {
return
}
var newNumber *big.Int
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)
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, calc.two)
newNumber = new(big.Int).Div(in, o.two)
}
calc.sequence.Iterations++
calc.sequence.Sequence = append(calc.sequence.Sequence, newNumber)
return calc.calculateNextCollatzNumber(*newNumber)
fmt.Printf(" -> %s", newNumber.Text(10))
o.nextNumber(newNumber)
}

View File

@ -1,48 +0,0 @@
package collatz
import (
"math/big"
"testing"
)
type MockParser struct {
value *big.Int
}
func (m *MockParser) Parse() (*big.Int, error) {
return m.value, nil
}
func TestNewCalculator(t *testing.T) {
parser := &MockParser{}
calculator := NewCalculator(parser)
if calculator.parser != parser {
t.Errorf("Expected parser to be %v, got %v", parser, calculator.parser)
}
if calculator.one.Cmp(big.NewInt(1)) != 0 {
t.Errorf("Expected one to be 1, got %v", calculator.one)
}
if calculator.two.Cmp(big.NewInt(2)) != 0 {
t.Errorf("Expected two to be 2, got %v", calculator.two)
}
if calculator.three.Cmp(big.NewInt(3)) != 0 {
t.Errorf("Expected three to be 3, got %v", calculator.three)
}
}
func TestCalculateCollatz(t *testing.T) {
parser := &MockParser{value: big.NewInt(6)}
calculator := NewCalculator(parser)
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

@ -1,4 +1,4 @@
package parsing
package io
import (
"errors"
@ -7,6 +7,7 @@ import (
)
type Cliargs struct {
value *big.Int
}
func (arg *Cliargs) Parse() (*big.Int, error) {
@ -18,5 +19,10 @@ 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
}

View File

@ -1,44 +0,0 @@
package parsing
import (
"math/big"
"os"
"testing"
)
func TestCliargs_Parse(t *testing.T) {
tests := []struct {
name string
args []string
want *big.Int
wantErr bool
}{
{
name: "Test with valid integer",
args: []string{"", "10"}, // the first argument is the program name
want: big.NewInt(10),
wantErr: false,
},
{
name: "Test with non-integer",
args: []string{"", "abc"}, // the first argument is the program name
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
parser := &Cliargs{}
got, err := parser.Parse()
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got.Cmp(tt.want) != 0 {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,7 +0,0 @@
package parsing
import "math/big"
type Parser interface {
Parse() (*big.Int, error)
}