diff --git a/52_permuted_multiples/main.go b/52_permuted_multiples/main.go new file mode 100644 index 0000000..51484c4 --- /dev/null +++ b/52_permuted_multiples/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +func main() { + i := 10 + for { + if isSameDigits([6]int{i, 2 * i, 3 * i, 4 * i, 5 * i, 6 * i}) { + break + } + i++ + } + fmt.Println("i=", i) +} + +func isSameDigits(value [6]int) bool { + last := 0 + for i, n := range value { + x := sortInt(n) + if i == 0 { + last = x + } else if x != last { + return false + } + } + return true +} + +func sortInt(input int) int { + swapped, _ := strconv.Atoi(bubbleSort(strconv.Itoa(input))) + return swapped + +} + +func bubbleSort(word string) string { + wordtable := strings.Split(word, "") + for j := 0; j < len(word); j++ { + + for i := 0; i < len(word)-1; i++ { + if wordtable[i] < wordtable[i+1] { + temp := wordtable[i] + wordtable[i] = wordtable[i+1] + wordtable[i+1] = temp + } + } + } + return strings.Join(wordtable, "") +} diff --git a/52_permuted_multiples/main_test.go b/52_permuted_multiples/main_test.go new file mode 100644 index 0000000..6a02c18 --- /dev/null +++ b/52_permuted_multiples/main_test.go @@ -0,0 +1,32 @@ +package main + +import "testing" + +func Test_isSameDigits(t *testing.T) { + type args struct { + value [6]int + } + tests := []struct { + name string + args args + want bool + }{ + { + "test1", + args{[6]int{2, 4, 6, 8, 10, 12}}, + false, + }, + { + "test2", + args{[6]int{142857, 285714, 428571, 571428, 714285, 857142}}, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isSameDigits(tt.args.value); got != tt.want { + t.Errorf("isSameDigits() = %v, want %v", got, tt.want) + } + }) + } +}