diff --git a/project_euler/problem_1/problem1.go b/project_euler/problem_1/problem1.go new file mode 100644 index 000000000..e41fbf468 --- /dev/null +++ b/project_euler/problem_1/problem1.go @@ -0,0 +1,24 @@ +/** + * Problem 1 - Multiples of 3 and 5 + * + * @see {@link https://projecteuler.net/problem=1} + * + * If we list all the natural numbers below 10 that are multiples of 3 or 5, + * we get 3, 5, 6 and 9. The sum of these multiples is 23. + * Find the sum of all the multiples of 3 or 5 below 1000. + * + * @author ddaniel27 + */ +package problem1 + +func Problem1(n uint) uint { + sum := uint(0) + + for i := uint(1); i < n; i++ { + if i%3 == 0 || i%5 == 0 { + sum += i + } + } + + return sum +} diff --git a/project_euler/problem_1/problem1_test.go b/project_euler/problem_1/problem1_test.go new file mode 100644 index 000000000..c2d649b02 --- /dev/null +++ b/project_euler/problem_1/problem1_test.go @@ -0,0 +1,45 @@ +package problem1 + +import "testing" + +// Tests +func TestProblem1_Func(t *testing.T) { + tests := []struct { + name string + threshold uint + want uint + }{ + { + name: "Testcase 1 - threshold 10", + threshold: 10, + want: 23, + }, + { + name: "Testcase 2 - threshold 100", + threshold: 100, + want: 2318, + }, + { + name: "Testcase 3 - threshold 1000", + threshold: 1000, + want: 233168, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem1(tt.threshold) + + if n != tt.want { + t.Errorf("Problem1() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem1(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem1(1000) + } +} diff --git a/project_euler/problem_10/problem10.go b/project_euler/problem_10/problem10.go new file mode 100644 index 000000000..342c79d24 --- /dev/null +++ b/project_euler/problem_10/problem10.go @@ -0,0 +1,23 @@ +/** +* Problem 10 - Summation of primes +* @see {@link https://projecteuler.net/problem=10} +* +* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. +* Find the sum of all the primes below two million. +* +* @author ddaniel27 + */ +package problem10 + +import "github.com/TheAlgorithms/Go/math/prime" + +func Problem10(n int) uint { + sum := uint(0) + sieve := prime.SieveEratosthenes(n) + + for _, v := range sieve { + sum += uint(v) + } + + return sum +} diff --git a/project_euler/problem_10/problem10_test.go b/project_euler/problem_10/problem10_test.go new file mode 100644 index 000000000..d82e6f29e --- /dev/null +++ b/project_euler/problem_10/problem10_test.go @@ -0,0 +1,40 @@ +package problem10 + +import "testing" + +// Tests +func TestProblem10_Func(t *testing.T) { + tests := []struct { + name string + input int + want uint + }{ + { + name: "Testcase 1 - input 10", + input: 10, + want: 17, + }, + { + name: "Testcase 2 - input 2000000", + input: 2000000, + want: 142913828922, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem10(tt.input) + + if n != tt.want { + t.Errorf("Problem10() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem10(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem10(2000000) + } +} diff --git a/project_euler/problem_11/problem11.go b/project_euler/problem_11/problem11.go new file mode 100644 index 000000000..5ddaea8e2 --- /dev/null +++ b/project_euler/problem_11/problem11.go @@ -0,0 +1,80 @@ +/** +* Problem 11 - Largest product in a grid +* @see {@link https://projecteuler.net/problem=11} +* +* In the 20×20 grid below, four numbers along a diagonal line have been marked in red. +* +* The product of these numbers is 26 × 63 × 78 × 14 = 1788696. +* +* What is the greatest product of four adjacent numbers in the same direction +* (up, down, left, right, or diagonally) in the 20×20 grid? +* +* @author ddaniel27 + */ +package problem11 + +var grid = [20][20]uint{ + {8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8}, + {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0}, + {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65}, + {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91}, + {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, + {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, + {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, + {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, + {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, + {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95}, + {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92}, + {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57}, + {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, + {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40}, + {4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, + {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, + {4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, + {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16}, + {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54}, + {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}, +} + +func Problem11() uint { + max := uint(0) + + for i := 0; i < 20; i++ { + for j := 0; j < 20; j++ { + + // Vertical + if i+3 < 20 { + product := grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid[i+3][j] + if product > max { + max = product + } + } + + // Horizontal + if j+3 < 20 { + product := grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3] + if product > max { + max = product + } + } + + if i+3 < 20 && j+3 < 20 { + // Diagonal + product := grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3] + if product > max { + max = product + } + } + + if i+3 < 20 && j-3 >= 0 { + // Diagonal + product := grid[i][j] * grid[i+1][j-1] * grid[i+2][j-2] * grid[i+3][j-3] + if product > max { + max = product + } + } + } + } + + return max +} diff --git a/project_euler/problem_11/problem11_test.go b/project_euler/problem_11/problem11_test.go new file mode 100644 index 000000000..b6ae99e05 --- /dev/null +++ b/project_euler/problem_11/problem11_test.go @@ -0,0 +1,29 @@ +package problem11 + +import "testing" + +// Tests +func TestProblem11_Func(t *testing.T) { + testCases := []struct { + name string + expected uint + }{ + {"Test Case 1", 70600674}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + actual := Problem11() + if actual != tc.expected { + t.Errorf("Expected: %v, but got %v", tc.expected, actual) + } + }) + } +} + +// Benchmark +func BenchmarkProblem11_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem11() + } +} diff --git a/project_euler/problem_12/problem12.go b/project_euler/problem_12/problem12.go new file mode 100644 index 000000000..803add5b7 --- /dev/null +++ b/project_euler/problem_12/problem12.go @@ -0,0 +1,46 @@ +/** +* Problem 12 - Highly divisible triangular number +* @see {@link https://projecteuler.net/problem=12} +* +* The sequence of triangle numbers is generated by adding the natural numbers. +* So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. +* The first ten terms would be: +* +* 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... +* +* Let us list the factors of the first seven triangle numbers: +* +* 1: 1 +* 3: 1,3 +* 6: 1,2,3,6 +* 10: 1,2,5,10 +* 15: 1,3,5,15 +* 21: 1,3,7,21 +* 28: 1,2,4,7,14,28 +* +* We can see that 28 is the first triangle number to have over five divisors. +* What is the value of the first triangle number to have over five hundred divisors? +* +* @author ddaniel27 + */ +package problem12 + +func Problem12(limit uint) uint { + triangle := uint(0) + for i := uint(1); ; i++ { + triangle += i + if numDivisors(triangle) >= limit { + return triangle + } + } +} + +func numDivisors(n uint) uint { + divisors := uint(0) + for i := uint(1); i*i <= n; i++ { + if n%i == 0 { + divisors += 2 + } + } + return divisors +} diff --git a/project_euler/problem_12/problem12_test.go b/project_euler/problem_12/problem12_test.go new file mode 100644 index 000000000..2659fb1e9 --- /dev/null +++ b/project_euler/problem_12/problem12_test.go @@ -0,0 +1,31 @@ +package problem12 + +import "testing" + +func TestProblem12_Func(t *testing.T) { + tests := []struct { + name string + input uint + want uint + }{ + {"Test Case 1", 6, 28}, + {"Test Case 2", 7, 36}, + {"Test Case 3", 11, 120}, + {"Test Case 4", 500, 76576500}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := Problem12(tt.input) + if actual != tt.want { + t.Errorf("Expected: %v, but got %v", tt.want, actual) + } + }) + } +} + +func BenchmarkProblem12_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem12(500) + } +} diff --git a/project_euler/problem_13/problem13.go b/project_euler/problem_13/problem13.go new file mode 100644 index 000000000..d5669d271 --- /dev/null +++ b/project_euler/problem_13/problem13.go @@ -0,0 +1,150 @@ +/** +* Problem 13 - Large sum +* @see {@link https://projecteuler.net/problem=13} +* +* Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. +* +* @author ddaniel27 + */ +package problem13 + +var numbers = [100]string{ + "37107287533902102798797998220837590246510135740250", + "46376937677490009712648124896970078050417018260538", + "74324986199524741059474233309513058123726617309629", + "91942213363574161572522430563301811072406154908250", + "23067588207539346171171980310421047513778063246676", + "89261670696623633820136378418383684178734361726757", + "28112879812849979408065481931592621691275889832738", + "44274228917432520321923589422876796487670272189318", + "47451445736001306439091167216856844588711603153276", + "70386486105843025439939619828917593665686757934951", + "62176457141856560629502157223196586755079324193331", + "64906352462741904929101432445813822663347944758178", + "92575867718337217661963751590579239728245598838407", + "58203565325359399008402633568948830189458628227828", + "80181199384826282014278194139940567587151170094390", + "35398664372827112653829987240784473053190104293586", + "86515506006295864861532075273371959191420517255829", + "71693888707715466499115593487603532921714970056938", + "54370070576826684624621495650076471787294438377604", + "53282654108756828443191190634694037855217779295145", + "36123272525000296071075082563815656710885258350721", + "45876576172410976447339110607218265236877223636045", + "17423706905851860660448207621209813287860733969412", + "81142660418086830619328460811191061556940512689692", + "51934325451728388641918047049293215058642563049483", + "62467221648435076201727918039944693004732956340691", + "15732444386908125794514089057706229429197107928209", + "55037687525678773091862540744969844508330393682126", + "18336384825330154686196124348767681297534375946515", + "80386287592878490201521685554828717201219257766954", + "78182833757993103614740356856449095527097864797581", + "16726320100436897842553539920931837441497806860984", + "48403098129077791799088218795327364475675590848030", + "87086987551392711854517078544161852424320693150332", + "59959406895756536782107074926966537676326235447210", + "69793950679652694742597709739166693763042633987085", + "41052684708299085211399427365734116182760315001271", + "65378607361501080857009149939512557028198746004375", + "35829035317434717326932123578154982629742552737307", + "94953759765105305946966067683156574377167401875275", + "88902802571733229619176668713819931811048770190271", + "25267680276078003013678680992525463401061632866526", + "36270218540497705585629946580636237993140746255962", + "24074486908231174977792365466257246923322810917141", + "91430288197103288597806669760892938638285025333403", + "34413065578016127815921815005561868836468420090470", + "23053081172816430487623791969842487255036638784583", + "11487696932154902810424020138335124462181441773470", + "63783299490636259666498587618221225225512486764533", + "67720186971698544312419572409913959008952310058822", + "95548255300263520781532296796249481641953868218774", + "76085327132285723110424803456124867697064507995236", + "37774242535411291684276865538926205024910326572967", + "23701913275725675285653248258265463092207058596522", + "29798860272258331913126375147341994889534765745501", + "18495701454879288984856827726077713721403798879715", + "38298203783031473527721580348144513491373226651381", + "34829543829199918180278916522431027392251122869539", + "40957953066405232632538044100059654939159879593635", + "29746152185502371307642255121183693803580388584903", + "41698116222072977186158236678424689157993532961922", + "62467957194401269043877107275048102390895523597457", + "23189706772547915061505504953922979530901129967519", + "86188088225875314529584099251203829009407770775672", + "11306739708304724483816533873502340845647058077308", + "82959174767140363198008187129011875491310547126581", + "97623331044818386269515456334926366572897563400500", + "42846280183517070527831839425882145521227251250327", + "55121603546981200581762165212827652751691296897789", + "32238195734329339946437501907836945765883352399886", + "75506164965184775180738168837861091527357929701337", + "62177842752192623401942399639168044983993173312731", + "32924185707147349566916674687634660915035914677504", + "99518671430235219628894890102423325116913619626622", + "73267460800591547471830798392868535206946944540724", + "76841822524674417161514036427982273348055556214818", + "97142617910342598647204516893989422179826088076852", + "87783646182799346313767754307809363333018982642090", + "10848802521674670883215120185883543223812876952786", + "71329612474782464538636993009049310363619763878039", + "62184073572399794223406235393808339651327408011116", + "66627891981488087797941876876144230030984490851411", + "60661826293682836764744779239180335110989069790714", + "85786944089552990653640447425576083659976645795096", + "66024396409905389607120198219976047599490197230297", + "64913982680032973156037120041377903785566085089252", + "16730939319872750275468906903707539413042652315011", + "94809377245048795150954100921645863754710598436791", + "78639167021187492431995700641917969777599028300699", + "15368713711936614952811305876380278410754449733078", + "40789923115535562561142322423255033685442488917353", + "44889911501440648020369068063960672322193204149535", + "41503128880339536053299340368006977710650566631954", + "81234880673210146739058568557934581403627822703280", + "82616570773948327592232845941706525094512325230608", + "22918802058777319719839450180888072429661980811197", + "77158542502016545090413245809786882778948721859617", + "72107838435069186155435662884062257473692284509516", + "20849603980134001723930671666823555245252804609722", + "53503534226472524250874054075591789781264330331690", +} + +func Problem13() string { + sum := "0" + + for _, n := range numbers { + sum = add(sum, n) + } + + return sum[:10] +} + +func add(a, b string) string { + if len(a) < len(b) { + a, b = b, a + } + + carry := 0 + sum := make([]byte, len(a)+1) + + for i := 0; i < len(a); i++ { + d := int(a[len(a)-1-i] - '0') + if i < len(b) { + d += int(b[len(b)-1-i] - '0') + } + d += carry + + sum[len(sum)-1-i] = byte(d%10) + '0' + carry = d / 10 + } + + if carry > 0 { + sum[0] = byte(carry) + '0' + } else { + sum = sum[1:] + } + + return string(sum) +} diff --git a/project_euler/problem_13/problem13_test.go b/project_euler/problem_13/problem13_test.go new file mode 100644 index 000000000..e776cbb04 --- /dev/null +++ b/project_euler/problem_13/problem13_test.go @@ -0,0 +1,27 @@ +package problem13 + +import "testing" + +func TestProblem13_Func(t *testing.T) { + tests := []struct { + name string + expected string + }{ + {"Test Case 1", "5537376230"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := Problem13() + if actual != tt.expected { + t.Errorf("Expected: %v, but got %v", tt.expected, actual) + } + }) + } +} + +func BenchmarkProblem13_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem13() + } +} diff --git a/project_euler/problem_14/problem14.go b/project_euler/problem_14/problem14.go new file mode 100644 index 000000000..aa5702c12 --- /dev/null +++ b/project_euler/problem_14/problem14.go @@ -0,0 +1,56 @@ +/** +* Problem 14 - Longest Collatz sequence +* @see {@link https://projecteuler.net/problem=14} +* +* The following iterative sequence is defined for the set of positive integers: +* n → n/2 (n is even) +* n → 3n + 1 (n is odd) +* +* Using the rule above and starting with 13, we generate the following sequence: +* 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 +* +* Which starting number, under one million, produces the longest chain? +* +* NOTE: Once the chain starts the terms are allowed to go above one million. +* +* @author ddaniel27 + */ +package problem14 + +type dict map[uint64]uint64 + +var dictionary = dict{ + 1: 1, +} + +func Problem14(limit uint64) uint64 { + for i := uint64(2); i <= limit; i++ { + weightNextNode(i) + } + + var max, maxWeight uint64 + for k, v := range dictionary { + if v > maxWeight { + max = k + maxWeight = v + } + } + + return max +} + +func weightNextNode(current uint64) uint64 { + var next, weight uint64 + if current%2 == 0 { + next = current / 2 + } else { + next = (3 * current) + 1 + } + if v, ok := dictionary[next]; !ok { + weight = weightNextNode(next) + 1 + } else { + weight = v + 1 + } + dictionary[current] = weight + return weight +} diff --git a/project_euler/problem_14/problem14_test.go b/project_euler/problem_14/problem14_test.go new file mode 100644 index 000000000..801ff0fdd --- /dev/null +++ b/project_euler/problem_14/problem14_test.go @@ -0,0 +1,31 @@ +package problem14 + +import "testing" + +// Tests +func TestProblem14_Func(t *testing.T) { + tests := []struct { + name string + input uint64 + want uint64 + }{ + {"Input 30", 30, 27}, + {"Input 1e6", 1e6, 837799}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := Problem14(tt.input) + if got != tt.want { + t.Errorf("Problem14() = %v, want %v", got, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem14_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem14(1e6) + } +} diff --git a/project_euler/problem_15/problem15.go b/project_euler/problem_15/problem15.go new file mode 100644 index 000000000..6adbd6977 --- /dev/null +++ b/project_euler/problem_15/problem15.go @@ -0,0 +1,42 @@ +/** +* Problem 15 - Lattice paths +* @see {@link https://projecteuler.net/problem=15} +* +* Starting in the top left corner of a 2×2 grid, +* and only being able to move to the right and down, +* there are exactly 6 routes to the bottom right corner. +* +* How many such routes are there through a 20×20 grid? +* +* @author ddaniel27 + */ +package problem15 + +import ( + "github.com/TheAlgorithms/Go/math/factorial" +) + +func Problem15(gridSize int) int { + /** + Author note: + We can solve this problem using combinatorics. + Here is a good blog post that explains the solution: + + [link](https://stemhash.com/counting-lattice-paths/) + + Btw, I'm not related to the author of the blog post. + + After some simplification, we can see that the solution is: + (2n)! / (n!)^2 + + We can use the factorial package to calculate the factorials. + */ + + n := gridSize + + numerator, _ := factorial.Iterative(2 * n) + denominator, _ := factorial.Iterative(n) + denominator *= denominator + + return numerator / denominator +} diff --git a/project_euler/problem_15/problem15_test.go b/project_euler/problem_15/problem15_test.go new file mode 100644 index 000000000..a5b729428 --- /dev/null +++ b/project_euler/problem_15/problem15_test.go @@ -0,0 +1,34 @@ +package problem15 + +import "testing" + +// Tests +func TestProblem15_Func(t *testing.T) { + tests := []struct { + name string + input int + want int + }{ + {"Input 2", 2, 6}, + // This test case is disabled + // because it needs a big integer to run successfully + // and factorial package doesn't support it + // {"Input 20", 20, 137846528820}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := Problem15(tt.input) + if got != tt.want { + t.Errorf("Problem15() = %v, want %v", got, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem15_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem15(20) + } +} diff --git a/project_euler/problem_16/problem16.go b/project_euler/problem_16/problem16.go new file mode 100644 index 000000000..cc0471c66 --- /dev/null +++ b/project_euler/problem_16/problem16.go @@ -0,0 +1,33 @@ +/** +* Problem 16 - Power digit sum +* @see {@link https://projecteuler.net/problem=16} +* +* 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. +* +* What is the sum of the digits of the number 2^1000? +* +* @author ddaniel27 + */ +package problem16 + +import ( + "math/big" +) + +func Problem16(exponent int64) int64 { + var result big.Int + + bigTwo := big.NewInt(2) + bigExponent := big.NewInt(exponent) + + result.Exp(bigTwo, bigExponent, nil) + + resultStr := result.String() + + var sum int64 + for _, digit := range resultStr { + sum += int64(digit - '0') + } + + return sum +} diff --git a/project_euler/problem_16/problem16_test.go b/project_euler/problem_16/problem16_test.go new file mode 100644 index 000000000..31e3f9a02 --- /dev/null +++ b/project_euler/problem_16/problem16_test.go @@ -0,0 +1,30 @@ +package problem16 + +import "testing" + +// Tests +func TestProblem16_Func(t *testing.T) { + tests := []struct { + name string + exponent int64 + want int64 + }{ + {"2^15", 15, 26}, + {"2^1000", 1000, 1366}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Problem16(tt.exponent); got != tt.want { + t.Errorf("Problem16() = %v, want %v", got, tt.want) + } + }) + } +} + +// Benchmark +func BenchmarkProblem16_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem16(1000) + } +} diff --git a/project_euler/problem_17/input.go b/project_euler/problem_17/input.go new file mode 100644 index 000000000..3a16f8c3a --- /dev/null +++ b/project_euler/problem_17/input.go @@ -0,0 +1,8 @@ +/** +* I put this code in a separate file because it is too long. +* Also it took me a lot of time to parsing this input from +* a random html page, so, I don't want to lose it. + */ +package problem17 + +const INPUT = "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Twenty one Twenty two Twenty three Twenty four Twenty five Twenty six Twenty seven Twenty eight Twenty nine Thirty Thirty one Thirty two Thirty three Thirty four Thirty five Thirty six Thirty seven Thirty eight Thirty nine Forty Forty one Forty two Forty three Forty four Forty five Forty six Forty seven Forty eight Forty nine Fifty Fifty one Fifty two Fifty three Fifty four Fifty five Fifty six Fifty seven Fifty eight Fifty nine Sixty Sixty one Sixty two Sixty three Sixty four Sixty five Sixty six Sixty seven Sixty eight Sixty nine Seventy Seventy one Seventy two Seventy three Seventy four Seventy five Seventy six Seventy seven Seventy eight Seventy nine Eighty Eighty one Eighty two Eighty three Eighty four Eighty five Eighty six Eighty seven Eighty eight Eighty nine Ninety Ninety one Ninety two Ninety three Ninety four Ninety five Ninety six Ninety seven Ninety eight Ninety nine One hundred One hundred and one One hundred and two One hundred and three One hundred and four One hundred and five One hundred and six One hundred and seven One hundred and eight One hundred and nine One hundred and ten One hundred and eleven One hundred and twelve One hundred and thirteen One hundred and fourteen One hundred and fifteen One hundred and sixteen One hundred and seventeen One hundred and eighteen One hundred and nineteen One hundred and twenty One hundred and twenty one One hundred and twenty two One hundred and twenty three One hundred and twenty four One hundred and twenty five One hundred and twenty six One hundred and twenty seven One hundred and twenty eight One hundred and twenty nine One hundred and thirty One hundred and thirty one One hundred and thirty two One hundred and thirty three One hundred and thirty four One hundred and thirty five One hundred and thirty six One hundred and thirty seven One hundred and thirty eight One hundred and thirty nine One hundred and forty One hundred and forty one One hundred and forty two One hundred and forty three One hundred and forty four One hundred and forty five One hundred and forty six One hundred and forty seven One hundred and forty eight One hundred and forty nine One hundred and fifty One hundred and fifty one One hundred and fifty two One hundred and fifty three One hundred and fifty four One hundred and fifty five One hundred and fifty six One hundred and fifty seven One hundred and fifty eight One hundred and fifty nine One hundred and sixty One hundred and sixty one One hundred and sixty two One hundred and sixty three One hundred and sixty four One hundred and sixty five One hundred and sixty six One hundred and sixty seven One hundred and sixty eight One hundred and sixty nine One hundred and seventy One hundred and seventy one One hundred and seventy two One hundred and seventy three One hundred and seventy four One hundred and seventy five One hundred and seventy six One hundred and seventy seven One hundred and seventy eight One hundred and seventy nine One hundred and eighty One hundred and eighty one One hundred and eighty two One hundred and eighty three One hundred and eighty four One hundred and eighty five One hundred and eighty six One hundred and eighty seven One hundred and eighty eight One hundred and eighty nine One hundred and ninety One hundred and ninety one One hundred and ninety two One hundred and ninety three One hundred and ninety four One hundred and ninety five One hundred and ninety six One hundred and ninety seven One hundred and ninety eight One hundred and ninety nine Two hundred Two hundred and one Two hundred and two Two hundred and three Two hundred and four Two hundred and five Two hundred and six Two hundred and seven Two hundred and eight Two hundred and nine Two hundred and ten Two hundred and eleven Two hundred and twelve Two hundred and thirteen Two hundred and fourteen Two hundred and fifteen Two hundred and sixteen Two hundred and seventeen Two hundred and eighteen Two hundred and nineteen Two hundred and twenty Two hundred and twenty one Two hundred and twenty two Two hundred and twenty three Two hundred and twenty four Two hundred and twenty five Two hundred and twenty six Two hundred and twenty seven Two hundred and twenty eight Two hundred and twenty nine Two hundred and thirty Two hundred and thirty one Two hundred and thirty two Two hundred and thirty three Two hundred and thirty four Two hundred and thirty five Two hundred and thirty six Two hundred and thirty seven Two hundred and thirty eight Two hundred and thirty nine Two hundred and forty Two hundred and forty one Two hundred and forty two Two hundred and forty three Two hundred and forty four Two hundred and forty five Two hundred and forty six Two hundred and forty seven Two hundred and forty eight Two hundred and forty nine Two hundred and fifty Two hundred and fifty one Two hundred and fifty two Two hundred and fifty three Two hundred and fifty four Two hundred and fifty five Two hundred and fifty six Two hundred and fifty seven Two hundred and fifty eight Two hundred and fifty nine Two hundred and sixty Two hundred and sixty one Two hundred and sixty two Two hundred and sixty three Two hundred and sixty four Two hundred and sixty five Two hundred and sixty six Two hundred and sixty seven Two hundred and sixty eight Two hundred and sixty nine Two hundred and seventy Two hundred and seventy one Two hundred and seventy two Two hundred and seventy three Two hundred and seventy four Two hundred and seventy five Two hundred and seventy six Two hundred and seventy seven Two hundred and seventy eight Two hundred and seventy nine Two hundred and eighty Two hundred and eighty one Two hundred and eighty two Two hundred and eighty three Two hundred and eighty four Two hundred and eighty five Two hundred and eighty six Two hundred and eighty seven Two hundred and eighty eight Two hundred and eighty nine Two hundred and ninety Two hundred and ninety one Two hundred and ninety two Two hundred and ninety three Two hundred and ninety four Two hundred and ninety five Two hundred and ninety six Two hundred and ninety seven Two hundred and ninety eight Two hundred and ninety nine Three hundred Three hundred and one Three hundred and two Three hundred and three Three hundred and four Three hundred and five Three hundred and six Three hundred and seven Three hundred and eight Three hundred and nine Three hundred and ten Three hundred and eleven Three hundred and twelve Three hundred and thirteen Three hundred and fourteen Three hundred and fifteen Three hundred and sixteen Three hundred and seventeen Three hundred and eighteen Three hundred and nineteen Three hundred and twenty Three hundred and twenty one Three hundred and twenty two Three hundred and twenty three Three hundred and twenty four Three hundred and twenty five Three hundred and twenty six Three hundred and twenty seven Three hundred and twenty eight Three hundred and twenty nine Three hundred and thirty Three hundred and thirty one Three hundred and thirty two Three hundred and thirty three Three hundred and thirty four Three hundred and thirty five Three hundred and thirty six Three hundred and thirty seven Three hundred and thirty eight Three hundred and thirty nine Three hundred and forty Three hundred and forty one Three hundred and forty two Three hundred and forty three Three hundred and forty four Three hundred and forty five Three hundred and forty six Three hundred and forty seven Three hundred and forty eight Three hundred and forty nine Three hundred and fifty Three hundred and fifty one Three hundred and fifty two Three hundred and fifty three Three hundred and fifty four Three hundred and fifty five Three hundred and fifty six Three hundred and fifty seven Three hundred and fifty eight Three hundred and fifty nine Three hundred and sixty Three hundred and sixty one Three hundred and sixty two Three hundred and sixty three Three hundred and sixty four Three hundred and sixty five Three hundred and sixty six Three hundred and sixty seven Three hundred and sixty eight Three hundred and sixty nine Three hundred and seventy Three hundred and seventy one Three hundred and seventy two Three hundred and seventy three Three hundred and seventy four Three hundred and seventy five Three hundred and seventy six Three hundred and seventy seven Three hundred and seventy eight Three hundred and seventy nine Three hundred and eighty Three hundred and eighty one Three hundred and eighty two Three hundred and eighty three Three hundred and eighty four Three hundred and eighty five Three hundred and eighty six Three hundred and eighty seven Three hundred and eighty eight Three hundred and eighty nine Three hundred and ninety Three hundred and ninety one Three hundred and ninety two Three hundred and ninety three Three hundred and ninety four Three hundred and ninety five Three hundred and ninety six Three hundred and ninety seven Three hundred and ninety eight Three hundred and ninety nine Four hundred Four hundred and one Four hundred and two Four hundred and three Four hundred and four Four hundred and five Four hundred and six Four hundred and seven Four hundred and eight Four hundred and nine Four hundred and ten Four hundred and eleven Four hundred and twelve Four hundred and thirteen Four hundred and fourteen Four hundred and fifteen Four hundred and sixteen Four hundred and seventeen Four hundred and eighteen Four hundred and nineteen Four hundred and twenty Four hundred and twenty one Four hundred and twenty two Four hundred and twenty three Four hundred and twenty four Four hundred and twenty five Four hundred and twenty six Four hundred and twenty seven Four hundred and twenty eight Four hundred and twenty nine Four hundred and thirty Four hundred and thirty one Four hundred and thirty two Four hundred and thirty three Four hundred and thirty four Four hundred and thirty five Four hundred and thirty six Four hundred and thirty seven Four hundred and thirty eight Four hundred and thirty nine Four hundred and forty Four hundred and forty one Four hundred and forty two Four hundred and forty three Four hundred and forty four Four hundred and forty five Four hundred and forty six Four hundred and forty seven Four hundred and forty eight Four hundred and forty nine Four hundred and fifty Four hundred and fifty one Four hundred and fifty two Four hundred and fifty three Four hundred and fifty four Four hundred and fifty five Four hundred and fifty six Four hundred and fifty seven Four hundred and fifty eight Four hundred and fifty nine Four hundred and sixty Four hundred and sixty one Four hundred and sixty two Four hundred and sixty three Four hundred and sixty four Four hundred and sixty five Four hundred and sixty six Four hundred and sixty seven Four hundred and sixty eight Four hundred and sixty nine Four hundred and seventy Four hundred and seventy one Four hundred and seventy two Four hundred and seventy three Four hundred and seventy four Four hundred and seventy five Four hundred and seventy six Four hundred and seventy seven Four hundred and seventy eight Four hundred and seventy nine Four hundred and eighty Four hundred and eighty one Four hundred and eighty two Four hundred and eighty three Four hundred and eighty four Four hundred and eighty five Four hundred and eighty six Four hundred and eighty seven Four hundred and eighty eight Four hundred and eighty nine Four hundred and ninety Four hundred and ninety one Four hundred and ninety two Four hundred and ninety three Four hundred and ninety four Four hundred and ninety five Four hundred and ninety six Four hundred and ninety seven Four hundred and ninety eight Four hundred and ninety nine Five hundred Five hundred and one Five hundred and two Five hundred and three Five hundred and four Five hundred and five Five hundred and six Five hundred and seven Five hundred and eight Five hundred and nine Five hundred and ten Five hundred and eleven Five hundred and twelve Five hundred and thirteen Five hundred and fourteen Five hundred and fifteen Five hundred and sixteen Five hundred and seventeen Five hundred and eighteen Five hundred and nineteen Five hundred and twenty Five hundred and twenty one Five hundred and twenty two Five hundred and twenty three Five hundred and twenty four Five hundred and twenty five Five hundred and twenty six Five hundred and twenty seven Five hundred and twenty eight Five hundred and twenty nine Five hundred and thirty Five hundred and thirty one Five hundred and thirty two Five hundred and thirty three Five hundred and thirty four Five hundred and thirty five Five hundred and thirty six Five hundred and thirty seven Five hundred and thirty eight Five hundred and thirty nine Five hundred and forty Five hundred and forty one Five hundred and forty two Five hundred and forty three Five hundred and forty four Five hundred and forty five Five hundred and forty six Five hundred and forty seven Five hundred and forty eight Five hundred and forty nine Five hundred and fifty Five hundred and fifty one Five hundred and fifty two Five hundred and fifty three Five hundred and fifty four Five hundred and fifty five Five hundred and fifty six Five hundred and fifty seven Five hundred and fifty eight Five hundred and fifty nine Five hundred and sixty Five hundred and sixty one Five hundred and sixty two Five hundred and sixty three Five hundred and sixty four Five hundred and sixty five Five hundred and sixty six Five hundred and sixty seven Five hundred and sixty eight Five hundred and sixty nine Five hundred and seventy Five hundred and seventy one Five hundred and seventy two Five hundred and seventy three Five hundred and seventy four Five hundred and seventy five Five hundred and seventy six Five hundred and seventy seven Five hundred and seventy eight Five hundred and seventy nine Five hundred and eighty Five hundred and eighty one Five hundred and eighty two Five hundred and eighty three Five hundred and eighty four Five hundred and eighty five Five hundred and eighty six Five hundred and eighty seven Five hundred and eighty eight Five hundred and eighty nine Five hundred and ninety Five hundred and ninety one Five hundred and ninety two Five hundred and ninety three Five hundred and ninety four Five hundred and ninety five Five hundred and ninety six Five hundred and ninety seven Five hundred and ninety eight Five hundred and ninety nine Six hundred Six hundred and one Six hundred and two Six hundred and three Six hundred and four Six hundred and five Six hundred and six Six hundred and seven Six hundred and eight Six hundred and nine Six hundred and ten Six hundred and eleven Six hundred and twelve Six hundred and thirteen Six hundred and fourteen Six hundred and fifteen Six hundred and sixteen Six hundred and seventeen Six hundred and eighteen Six hundred and nineteen Six hundred and twenty Six hundred and twenty one Six hundred and twenty two Six hundred and twenty three Six hundred and twenty four Six hundred and twenty five Six hundred and twenty six Six hundred and twenty seven Six hundred and twenty eight Six hundred and twenty nine Six hundred and thirty Six hundred and thirty one Six hundred and thirty two Six hundred and thirty three Six hundred and thirty four Six hundred and thirty five Six hundred and thirty six Six hundred and thirty seven Six hundred and thirty eight Six hundred and thirty nine Six hundred and forty Six hundred and forty one Six hundred and forty two Six hundred and forty three Six hundred and forty four Six hundred and forty five Six hundred and forty six Six hundred and forty seven Six hundred and forty eight Six hundred and forty nine Six hundred and fifty Six hundred and fifty one Six hundred and fifty two Six hundred and fifty three Six hundred and fifty four Six hundred and fifty five Six hundred and fifty six Six hundred and fifty seven Six hundred and fifty eight Six hundred and fifty nine Six hundred and sixty Six hundred and sixty one Six hundred and sixty two Six hundred and sixty three Six hundred and sixty four Six hundred and sixty five Six hundred and sixty six Six hundred and sixty seven Six hundred and sixty eight Six hundred and sixty nine Six hundred and seventy Six hundred and seventy one Six hundred and seventy two Six hundred and seventy three Six hundred and seventy four Six hundred and seventy five Six hundred and seventy six Six hundred and seventy seven Six hundred and seventy eight Six hundred and seventy nine Six hundred and eighty Six hundred and eighty one Six hundred and eighty two Six hundred and eighty three Six hundred and eighty four Six hundred and eighty five Six hundred and eighty six Six hundred and eighty seven Six hundred and eighty eight Six hundred and eighty nine Six hundred and ninety Six hundred and ninety one Six hundred and ninety two Six hundred and ninety three Six hundred and ninety four Six hundred and ninety five Six hundred and ninety six Six hundred and ninety seven Six hundred and ninety eight Six hundred and ninety nine Seven hundred Seven hundred and one Seven hundred and two Seven hundred and three Seven hundred and four Seven hundred and five Seven hundred and six Seven hundred and seven Seven hundred and eight Seven hundred and nine Seven hundred and ten Seven hundred and eleven Seven hundred and twelve Seven hundred and thirteen Seven hundred and fourteen Seven hundred and fifteen Seven hundred and sixteen Seven hundred and seventeen Seven hundred and eighteen Seven hundred and nineteen Seven hundred and twenty Seven hundred and twenty one Seven hundred and twenty two Seven hundred and twenty three Seven hundred and twenty four Seven hundred and twenty five Seven hundred and twenty six Seven hundred and twenty seven Seven hundred and twenty eight Seven hundred and twenty nine Seven hundred and thirty Seven hundred and thirty one Seven hundred and thirty two Seven hundred and thirty three Seven hundred and thirty four Seven hundred and thirty five Seven hundred and thirty six Seven hundred and thirty seven Seven hundred and thirty eight Seven hundred and thirty nine Seven hundred and forty Seven hundred and forty one Seven hundred and forty two Seven hundred and forty three Seven hundred and forty four Seven hundred and forty five Seven hundred and forty six Seven hundred and forty seven Seven hundred and forty eight Seven hundred and forty nine Seven hundred and fifty Seven hundred and fifty one Seven hundred and fifty two Seven hundred and fifty three Seven hundred and fifty four Seven hundred and fifty five Seven hundred and fifty six Seven hundred and fifty seven Seven hundred and fifty eight Seven hundred and fifty nine Seven hundred and sixty Seven hundred and sixty one Seven hundred and sixty two Seven hundred and sixty three Seven hundred and sixty four Seven hundred and sixty five Seven hundred and sixty six Seven hundred and sixty seven Seven hundred and sixty eight Seven hundred and sixty nine Seven hundred and seventy Seven hundred and seventy one Seven hundred and seventy two Seven hundred and seventy three Seven hundred and seventy four Seven hundred and seventy five Seven hundred and seventy six Seven hundred and seventy seven Seven hundred and seventy eight Seven hundred and seventy nine Seven hundred and eighty Seven hundred and eighty one Seven hundred and eighty two Seven hundred and eighty three Seven hundred and eighty four Seven hundred and eighty five Seven hundred and eighty six Seven hundred and eighty seven Seven hundred and eighty eight Seven hundred and eighty nine Seven hundred and ninety Seven hundred and ninety one Seven hundred and ninety two Seven hundred and ninety three Seven hundred and ninety four Seven hundred and ninety five Seven hundred and ninety six Seven hundred and ninety seven Seven hundred and ninety eight Seven hundred and ninety nine Eight hundred Eight hundred and one Eight hundred and two Eight hundred and three Eight hundred and four Eight hundred and five Eight hundred and six Eight hundred and seven Eight hundred and eight Eight hundred and nine Eight hundred and ten Eight hundred and eleven Eight hundred and twelve Eight hundred and thirteen Eight hundred and fourteen Eight hundred and fifteen Eight hundred and sixteen Eight hundred and seventeen Eight hundred and eighteen Eight hundred and nineteen Eight hundred and twenty Eight hundred and twenty one Eight hundred and twenty two Eight hundred and twenty three Eight hundred and twenty four Eight hundred and twenty five Eight hundred and twenty six Eight hundred and twenty seven Eight hundred and twenty eight Eight hundred and twenty nine Eight hundred and thirty Eight hundred and thirty one Eight hundred and thirty two Eight hundred and thirty three Eight hundred and thirty four Eight hundred and thirty five Eight hundred and thirty six Eight hundred and thirty seven Eight hundred and thirty eight Eight hundred and thirty nine Eight hundred and forty Eight hundred and forty one Eight hundred and forty two Eight hundred and forty three Eight hundred and forty four Eight hundred and forty five Eight hundred and forty six Eight hundred and forty seven Eight hundred and forty eight Eight hundred and forty nine Eight hundred and fifty Eight hundred and fifty one Eight hundred and fifty two Eight hundred and fifty three Eight hundred and fifty four Eight hundred and fifty five Eight hundred and fifty six Eight hundred and fifty seven Eight hundred and fifty eight Eight hundred and fifty nine Eight hundred and sixty Eight hundred and sixty one Eight hundred and sixty two Eight hundred and sixty three Eight hundred and sixty four Eight hundred and sixty five Eight hundred and sixty six Eight hundred and sixty seven Eight hundred and sixty eight Eight hundred and sixty nine Eight hundred and seventy Eight hundred and seventy one Eight hundred and seventy two Eight hundred and seventy three Eight hundred and seventy four Eight hundred and seventy five Eight hundred and seventy six Eight hundred and seventy seven Eight hundred and seventy eight Eight hundred and seventy nine Eight hundred and eighty Eight hundred and eighty one Eight hundred and eighty two Eight hundred and eighty three Eight hundred and eighty four Eight hundred and eighty five Eight hundred and eighty six Eight hundred and eighty seven Eight hundred and eighty eight Eight hundred and eighty nine Eight hundred and ninety Eight hundred and ninety one Eight hundred and ninety two Eight hundred and ninety three Eight hundred and ninety four Eight hundred and ninety five Eight hundred and ninety six Eight hundred and ninety seven Eight hundred and ninety eight Eight hundred and ninety nine Nine hundred Nine hundred and one Nine hundred and two Nine hundred and three Nine hundred and four Nine hundred and five Nine hundred and six Nine hundred and seven Nine hundred and eight Nine hundred and nine Nine hundred and ten Nine hundred and eleven Nine hundred and twelve Nine hundred and thirteen Nine hundred and fourteen Nine hundred and fifteen Nine hundred and sixteen Nine hundred and seventeen Nine hundred and eighteen Nine hundred and nineteen Nine hundred and twenty Nine hundred and twenty one Nine hundred and twenty two Nine hundred and twenty three Nine hundred and twenty four Nine hundred and twenty five Nine hundred and twenty six Nine hundred and twenty seven Nine hundred and twenty eight Nine hundred and twenty nine Nine hundred and thirty Nine hundred and thirty one Nine hundred and thirty two Nine hundred and thirty three Nine hundred and thirty four Nine hundred and thirty five Nine hundred and thirty six Nine hundred and thirty seven Nine hundred and thirty eight Nine hundred and thirty nine Nine hundred and forty Nine hundred and forty one Nine hundred and forty two Nine hundred and forty three Nine hundred and forty four Nine hundred and forty five Nine hundred and forty six Nine hundred and forty seven Nine hundred and forty eight Nine hundred and forty nine Nine hundred and fifty Nine hundred and fifty one Nine hundred and fifty two Nine hundred and fifty three Nine hundred and fifty four Nine hundred and fifty five Nine hundred and fifty six Nine hundred and fifty seven Nine hundred and fifty eight Nine hundred and fifty nine Nine hundred and sixty Nine hundred and sixty one Nine hundred and sixty two Nine hundred and sixty three Nine hundred and sixty four Nine hundred and sixty five Nine hundred and sixty six Nine hundred and sixty seven Nine hundred and sixty eight Nine hundred and sixty nine Nine hundred and seventy Nine hundred and seventy one Nine hundred and seventy two Nine hundred and seventy three Nine hundred and seventy four Nine hundred and seventy five Nine hundred and seventy six Nine hundred and seventy seven Nine hundred and seventy eight Nine hundred and seventy nine Nine hundred and eighty Nine hundred and eighty one Nine hundred and eighty two Nine hundred and eighty three Nine hundred and eighty four Nine hundred and eighty five Nine hundred and eighty six Nine hundred and eighty seven Nine hundred and eighty eight Nine hundred and eighty nine Nine hundred and ninety Nine hundred and ninety one Nine hundred and ninety two Nine hundred and ninety three Nine hundred and ninety four Nine hundred and ninety five Nine hundred and ninety six Nine hundred and ninety seven Nine hundred and ninety eight Nine hundred and ninety nine One thousand" diff --git a/project_euler/problem_17/problem17.go b/project_euler/problem_17/problem17.go new file mode 100644 index 000000000..a5e2beee7 --- /dev/null +++ b/project_euler/problem_17/problem17.go @@ -0,0 +1,31 @@ +/** +* Problem 17 - Number letter counts +* @see {@link https://projecteuler.net/problem=17} +* +* If the numbers 1 to 5 are written out in words: one, two, three, four, five, +* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. +* +* If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, +* how many letters would be used? +* +* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) +* contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. +* The use of "and" when writing out numbers is in compliance with British usage. +* +* @author ddaniel27 + */ +package problem17 + +import "strings" + +func Problem17(input string) int { + var sum int + + parsed := strings.Split(input, " ") + + for _, word := range parsed { + sum += len(word) + } + + return sum +} diff --git a/project_euler/problem_17/problem17_test.go b/project_euler/problem_17/problem17_test.go new file mode 100644 index 000000000..13cf68bf4 --- /dev/null +++ b/project_euler/problem_17/problem17_test.go @@ -0,0 +1,30 @@ +package problem17 + +import "testing" + +// Tests +func TestProblem17_Func(t *testing.T) { + tests := []struct { + name string + input string + want int + }{ + {"1 to 5", "one two three four five", 19}, + {"1 to 1000", INPUT, 21124}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Problem17(tt.input); got != tt.want { + t.Errorf("Problem17() = %v, want %v", got, tt.want) + } + }) + } +} + +// Benchmark +func BenchmarkProblem17_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem17(INPUT) + } +} diff --git a/project_euler/problem_18/edge.go b/project_euler/problem_18/edge.go new file mode 100644 index 000000000..90f44b082 --- /dev/null +++ b/project_euler/problem_18/edge.go @@ -0,0 +1,100 @@ +package problem18 + +type Edge struct { + ID int + NodeValue NodeValue + NodeLeft Node + NodeRight Node + Parent Node +} + +func (n *Edge) Value() NodeValue { + return n.NodeValue +} + +func (n *Edge) Left() Node { + return n.NodeLeft +} + +func (n *Edge) Right() Node { + return n.NodeRight +} + +func (n *Edge) Kind() string { + return "edge" +} + +func (n *Edge) CreateChild(value NodeValue, id int) Node { + // When the left child is nil, it's a left edge + if n.NodeLeft == nil { + return &Edge{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } + } + + // When the left child is a leaf, it's a right edge + if n.NodeLeft.Kind() == "leaf" { + return &Edge{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } + } + + return &Leaf{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } +} + +func (n *Edge) GetID() int { + return n.ID +} + +func (n *Edge) Insert(node Node) { + // If Left is nil, always simply insert the node + if n.NodeLeft == nil { + node.SetParent(n) + n.NodeLeft = node + + return + } + + // If Right is nil, insert the node + n.NodeRight = node + + // If the node to insert is an edge, set the parent + if node.Kind() == "edge" { + node.SetParent(n) + + return + } + + // If the node to insert is a leaf, send it to the sibling right + n.Parent.Right().Insert(node) +} + +func (n *Edge) HasSpace() bool { + return n.NodeLeft == nil || n.NodeRight == nil +} + +func (n *Edge) LeftIsNil() bool { + return n.NodeLeft == nil +} + +func (n *Edge) RightIsNil() bool { + return n.NodeRight == nil +} + +func (n *Edge) SetParent(node Node) { + n.Parent = node +} diff --git a/project_euler/problem_18/input.go b/project_euler/problem_18/input.go new file mode 100644 index 000000000..e11dcc41f --- /dev/null +++ b/project_euler/problem_18/input.go @@ -0,0 +1,42 @@ +package problem18 + +import "strings" + +const problem18_input_string = ` +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 +` + +var problem18_input_parsed_string []string = strings.Split( + strings.Trim( + strings.ReplaceAll(problem18_input_string, "\n", " "), + " ", + ), + " ") + +const problem18_test_string = ` +3 +7 4 +2 4 6 +8 5 9 3 +` + +var problem18_test_parsed_string []string = strings.Split( + strings.Trim( + strings.ReplaceAll(problem18_test_string, "\n", " "), + " ", + ), + " ") diff --git a/project_euler/problem_18/leaf.go b/project_euler/problem_18/leaf.go new file mode 100644 index 000000000..8dd452602 --- /dev/null +++ b/project_euler/problem_18/leaf.go @@ -0,0 +1,75 @@ +package problem18 + +type Leaf struct { + ID int + NodeValue NodeValue + NodeLeft *Leaf + NodeRight *Leaf + Parent Node +} + +func (n *Leaf) Value() NodeValue { + return n.NodeValue +} + +func (n *Leaf) Left() Node { + if n.NodeLeft != nil { + n.NodeLeft.Parent = n // Leaf is the parent of its left child always + } + + return n.NodeLeft +} + +func (n *Leaf) Right() Node { + return n.NodeRight +} + +func (n *Leaf) Kind() string { + return "leaf" +} + +func (n *Leaf) CreateChild(value NodeValue, id int) Node { + // Leafs only have leaf children + return &Leaf{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } +} + +func (n *Leaf) GetID() int { + return n.ID +} + +func (n *Leaf) Insert(node Node) { + // If Left is nil, always simply insert the node + if n.NodeLeft == nil { + node.SetParent(n) + n.NodeLeft = node.(*Leaf) + + return + } + + // If Right is nil, insert the node + n.NodeRight = node.(*Leaf) + // Send it to the sibling right + n.Parent.Right().Insert(node) +} + +func (n *Leaf) HasSpace() bool { + return n.NodeLeft == nil || n.NodeRight == nil +} + +func (n *Leaf) LeftIsNil() bool { + return n.NodeLeft == nil +} + +func (n *Leaf) RightIsNil() bool { + return n.NodeRight == nil +} + +func (n *Leaf) SetParent(node Node) { + n.Parent = node +} diff --git a/project_euler/problem_18/problem18.go b/project_euler/problem_18/problem18.go new file mode 100644 index 000000000..1775c6bd0 --- /dev/null +++ b/project_euler/problem_18/problem18.go @@ -0,0 +1,63 @@ +/** +* Problem 18 - Maximum path sum I +* @see {@link https://projecteuler.net/problem=18} +* +* By starting at the top of the triangle below and +* moving to adjacent numbers on the row below, +* the maximum total from top to bottom is 23. +* +* 3 +* 7 4 +* 2 4 6 +* 8 5 9 3 +* +* That is, 3 + 7 + 4 + 9 = 23. +* +* Find the maximum total from top to bottom of the triangle below: +* [refer to the problem link] +* +* NOTE: As there are only 16384 routes, it is possible +* to solve this problem by trying every route. +* However, Problem 67, is the same challenge with a triangle +* containing one-hundred rows; it cannot be solved by brute force, +* and requires a clever method! ;o) +* +* @author ddaniel27 + */ +package problem18 + +import "strconv" + +type ( + NodeValue int + NodeType string + + Node interface { + Value() NodeValue + GetID() int + Left() Node + Right() Node + LeftIsNil() bool + RightIsNil() bool + HasSpace() bool + Kind() string + SetParent(Node) + CreateChild(NodeValue, int) Node + Insert(Node) + } +) + +func Problem18(input []string, deep int) int { + tree := &Tree{} + + for _, num := range input { + v, err := strconv.Atoi(num) + if err != nil { + panic(err) + } + + tree.BFSInsert(NodeValue(v)) + } + + return tree.MaxPathValueSearch(deep) +} diff --git a/project_euler/problem_18/problem18_test.go b/project_euler/problem_18/problem18_test.go new file mode 100644 index 000000000..50baab2ed --- /dev/null +++ b/project_euler/problem_18/problem18_test.go @@ -0,0 +1,42 @@ +package problem18 + +import "testing" + +// Tests +func TestProblem18_Func(t *testing.T) { + tests := []struct { + name string + input []string + deep int + expected int + }{ + { + name: "Test case 1", + input: problem18_test_parsed_string, + deep: 2, + expected: 23, + }, + { + name: "Test case 2", + input: problem18_input_parsed_string, + deep: 2, + expected: 1074, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual := Problem18(test.input, test.deep) + if actual != test.expected { + t.Errorf("Expected %d, but got %d", test.expected, actual) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem18_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem18(problem18_input_parsed_string, 2) + } +} diff --git a/project_euler/problem_18/root.go b/project_euler/problem_18/root.go new file mode 100644 index 000000000..4ee018629 --- /dev/null +++ b/project_euler/problem_18/root.go @@ -0,0 +1,62 @@ +package problem18 + +type Root struct { + ID int + NodeValue NodeValue + NodeLeft *Edge + NodeRight *Edge +} + +func (n *Root) Value() NodeValue { + return n.NodeValue +} + +func (n *Root) Left() Node { + return n.NodeLeft +} + +func (n *Root) Right() Node { + return n.NodeRight +} + +func (n *Root) Kind() string { + return "root" +} + +func (n *Root) CreateChild(value NodeValue, id int) Node { + return &Edge{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } +} + +func (n *Root) GetID() int { + return n.ID +} + +func (n *Root) Insert(node Node) { + if n.NodeLeft == nil { + n.NodeLeft = node.(*Edge) + } else { + n.NodeRight = node.(*Edge) + } +} + +func (n *Root) HasSpace() bool { + return n.NodeLeft == nil || n.NodeRight == nil +} + +func (n *Root) LeftIsNil() bool { + return n.NodeLeft == nil +} + +func (n *Root) RightIsNil() bool { + return n.NodeRight == nil +} + +func (n *Root) SetParent(node Node) { + panic("Root node cannot have a parent") +} diff --git a/project_euler/problem_18/tree.go b/project_euler/problem_18/tree.go new file mode 100644 index 000000000..6f98f3b1c --- /dev/null +++ b/project_euler/problem_18/tree.go @@ -0,0 +1,229 @@ +package problem18 + +import ( + "fmt" + "strings" +) + +type Tree struct { + Root *Root + Nodes map[int]struct{} + ID int +} + +func (t *Tree) BFSInsert(value NodeValue) { + t.Nodes = make(map[int]struct{}) // Reset the nodes map + + if t.Root == nil { + t.Root = &Root{NodeValue: value, ID: 0} + t.ID = 1 + return + } + + queue := make([]Node, 0) + queue = append(queue, t.Root) + t.isInQueue(t.Root.GetID()) + + head := 0 + + for head < len(queue) { + current := queue[head] + head++ + childNode := current.CreateChild(value, t.ID) + + if current.HasSpace() { + current.Insert(childNode) + t.ID++ + return + } + + if !t.isInQueue(current.Left().GetID()) { // Avoid duplicates + queue = append(queue, current.Left()) + } + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + } + } +} + +// MaxPathValueSearch is a method that searches the maximum path value in a tree +// given a certain depth +func (r *Tree) MaxPathValueSearch(deep int) int { + if r.Root == nil { + return 0 + } + + type DFSNode struct { + Node Node + StepsLeft int + Sum int + } + + var pivot Node = r.Root + pivotEnded := false + maxPathValue := int(pivot.Value()) + + stack := make([]DFSNode, 0) + + for !pivotEnded { + stack = append(stack, DFSNode{Node: pivot, StepsLeft: deep, Sum: maxPathValue}) + + for len(stack) > 0 { + current := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + // If we run out of steps, we check the sum of the path, + // update the maxPathValue if necessary and continue + if current.StepsLeft == 0 { + if current.Sum > maxPathValue { + maxPathValue = current.Sum + pivot = current.Node + } + continue + } + + if !current.Node.RightIsNil() { + stack = append(stack, DFSNode{ + Node: current.Node.Right(), + StepsLeft: current.StepsLeft - 1, + Sum: current.Sum + int(current.Node.Right().Value()), + }) + } + + // If the left child is nil, we have reached the end of the path + if !current.Node.LeftIsNil() { + stack = append(stack, DFSNode{ + Node: current.Node.Left(), + StepsLeft: current.StepsLeft - 1, + Sum: current.Sum + int(current.Node.Left().Value()), + }) + } else { + if current.Sum > maxPathValue { + maxPathValue = current.Sum + pivot = current.Node + } + } + } + + // If we don't have reached the end of the left side of the tree, + // we continue with the search using the pivot node + // We use the left child only because how the tree is built + if pivot.LeftIsNil() { + pivotEnded = true + } + } + + return maxPathValue +} + +// PrintReport is a method that prints a report of the tree +// Node by node +func (t *Tree) PrintReport() { + t.Nodes = make(map[int]struct{}) + if t.Root == nil { + return + } + + queue := make([]Node, 0) + queue = append(queue, t.Root) + + head := 0 + + for head < len(queue) { + current := queue[head] + head++ + + print("ID:", current.GetID()) + print(", Current node:", current.Value()) + + if !current.LeftIsNil() { + print(", Left Child:", current.Left().Value()) + + if !t.isInQueue(current.Left().GetID()) { + queue = append(queue, current.Left()) + } + } + + if !current.RightIsNil() { + print(", Right Child:", current.Right().Value()) + + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + } + } + + println() + } +} + +// PrintPyramid is a method that prints the tree as a pyramid +func (t *Tree) PrintPyramid() { + t.Nodes = make(map[int]struct{}) + if t.Root == nil { + return + } + + queue := []Node{t.Root} + levels := []int{0} + outputByLevel := []string{} + + head := 0 + currentLevel := 0 + + output := "" + + for head < len(queue) { + current := queue[head] + level := levels[head] + head++ + + // Level change + if level > currentLevel { + currentLevel = level + outputByLevel = append(outputByLevel, output+"\n") + output = "" + } + + // Add current node to the output + if current.Value() < 10 { + output += fmt.Sprintf("0%d ", current.Value()) + } else { + output += fmt.Sprintf("%d ", current.Value()) + } + + // Add children to the queue + if !current.LeftIsNil() { + if !t.isInQueue(current.Left().GetID()) { + queue = append(queue, current.Left()) + levels = append(levels, level+1) + } + } + + if !current.RightIsNil() { + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + levels = append(levels, level+1) + } + } + } + // Add the last level + outputByLevel = append(outputByLevel, output+"\n") + + totalLevels := len(outputByLevel) + + // Print the pyramid + for i, level := range outputByLevel { + str := strings.Repeat(" ", 2*(totalLevels-i)) + level + print(str) + } +} + +// isInQueue is a method that avoids duplicates in the tree +func (n *Tree) isInQueue(nv int) bool { + if _, ok := n.Nodes[nv]; ok { + return true + } + + n.Nodes[nv] = struct{}{} + return false +} diff --git a/project_euler/problem_19/problem19.go b/project_euler/problem_19/problem19.go new file mode 100644 index 000000000..9a86f0dea --- /dev/null +++ b/project_euler/problem_19/problem19.go @@ -0,0 +1,63 @@ +package problem19 + +/** +* Problem 19 - Counting Sundays +* @see {@link https://projecteuler.net/problem=19} +* +* You are given the following information, +* but you may prefer to do some research for yourself. +* +* 1 Jan 1900 was a Monday. +* Thirty days has September, +* April, June and November. +* All the rest have thirty-one, +* Saving February alone, +* Which has twenty-eight, rain or shine. +* And on leap years, twenty-nine. +* A leap year occurs on any year evenly divisible by 4, +* but not on a century unless it is divisible by 400. +* +* How many Sundays fell on the first of the month during +* the twentieth century (1 Jan 1901 to 31 Dec 2000)? +* +* @author ddaniel27 + */ + +func Problem19() int { + count := 0 + dayOfWeek := 2 // 1 Jan 1901 was a Tuesday + + for year := 1901; year <= 2000; year++ { + for month := 1; month <= 12; month++ { + if dayOfWeek == 0 { + count++ + } + + daysInMonth := 31 + switch month { + case 4, 6, 9, 11: + daysInMonth = 30 + case 2: + if IsLeapYear(year) { + daysInMonth = 29 + } else { + daysInMonth = 28 + } + } + + dayOfWeek = (dayOfWeek + daysInMonth) % 7 + } + } + + return count +} + +func IsLeapYear(year int) bool { + if year%4 == 0 { + if year%100 == 0 { + return year%400 == 0 + } + return true + } + return false +} diff --git a/project_euler/problem_19/problem19_test.go b/project_euler/problem_19/problem19_test.go new file mode 100644 index 000000000..d26c10938 --- /dev/null +++ b/project_euler/problem_19/problem19_test.go @@ -0,0 +1,29 @@ +package problem19 + +import "testing" + +// Tests +func TestProblem19_Func(t *testing.T) { + tests := []struct { + name string + expected int + }{ + {"Problem 19 - Counting Sundays", 171}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := Problem19() + if got != test.expected { + t.Errorf("Problem19() = got %v, want %v", got, test.expected) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem19_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem19() + } +} diff --git a/project_euler/problem_2/problem2.go b/project_euler/problem_2/problem2.go new file mode 100644 index 000000000..e8eaeb85e --- /dev/null +++ b/project_euler/problem_2/problem2.go @@ -0,0 +1,30 @@ +/** +* Problem 2 - Even Fibonacci numbers +* @see {@link https://projecteuler.net/problem=2} +* +* Each new term in the Fibonacci sequence is generated by adding the previous two terms. +* By starting with 1 and 2, the first 10 terms will be: +* +* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... +* +* By considering the terms in the Fibonacci sequence whose values do not exceed four million, +* find the sum of the even-valued terms. +* +* @author ddaniel27 + */ +package problem2 + +func Problem2(n uint) uint { + sum := uint(0) + a, b := uint(1), uint(2) + + for b < n { + if b%2 == 0 { + sum += b + } + + a, b = b, a+b + } + + return sum +} diff --git a/project_euler/problem_2/problem2_test.go b/project_euler/problem_2/problem2_test.go new file mode 100644 index 000000000..32c3834c6 --- /dev/null +++ b/project_euler/problem_2/problem2_test.go @@ -0,0 +1,45 @@ +package problem2 + +import "testing" + +// Tests +func TestProblem2_Func(t *testing.T) { + tests := []struct { + name string + input uint + want uint + }{ + { + name: "Testcase 1 - input 10", + input: 10, + want: 10, + }, + { + name: "Testcase 2 - input 100", + input: 100, + want: 44, + }, + { + name: "Testcase 3 - input 4e6", + input: 4e6, + want: 4613732, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem2(tt.input) + + if n != tt.want { + t.Errorf("Problem2() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem2(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem2(4e6) + } +} diff --git a/project_euler/problem_20/problem20.go b/project_euler/problem_20/problem20.go new file mode 100644 index 000000000..fc731da6b --- /dev/null +++ b/project_euler/problem_20/problem20.go @@ -0,0 +1,37 @@ +/** +* Problem 20 - Factorial digit sum +* @see {@link https://projecteuler.net/problem=20} +* +* n! means n × (n − 1) × ... × 3 × 2 × 1 +* +* For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +* and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. +* +* Find the sum of the digits in the number 100! +* +* @author ddaniel27 + */ +package problem20 + +import "math/big" + +func Problem20(input int) int { + factorial := bigFactorial(input) + sum := 0 + for _, digit := range factorial.String() { + sum += int(digit - '0') + } + return sum +} + +// bigFactorial returns the factorial of n as a big.Int +// Use big package to handle large numbers +func bigFactorial(n int) *big.Int { + if n < 0 { + return big.NewInt(0) + } + if n == 0 { + return big.NewInt(1) + } + return big.NewInt(0).Mul(big.NewInt(int64(n)), bigFactorial(n-1)) +} diff --git a/project_euler/problem_20/problem20_test.go b/project_euler/problem_20/problem20_test.go new file mode 100644 index 000000000..f1ac574c6 --- /dev/null +++ b/project_euler/problem_20/problem20_test.go @@ -0,0 +1,31 @@ +package problem20 + +import "testing" + +// Tests +func TestProblem20_Func(t *testing.T) { + tests := []struct { + name string + input int + expected int + }{ + {"Problem 20 - Factorial digit sum", 10, 27}, + {"Problem 20 - Factorial digit sum", 100, 648}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := Problem20(test.input) + if got != test.expected { + t.Errorf("Problem20() = got %v, want %v", got, test.expected) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem20_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem20(100) + } +} diff --git a/project_euler/problem_3/problem3.go b/project_euler/problem_3/problem3.go new file mode 100644 index 000000000..f13525fb6 --- /dev/null +++ b/project_euler/problem_3/problem3.go @@ -0,0 +1,24 @@ +/** +* Problem 3 - Largest prime factor +* @see {@link https://projecteuler.net/problem=3} +* +* The prime factors of 13195 are 5, 7, 13 and 29. +* What is the largest prime factor of the number 600851475143 ? +* +* @author ddaniel27 + */ +package problem3 + +func Problem3(n uint) uint { + i := uint(2) + + for n > 1 { + if n%i == 0 { + n /= i + } else { + i++ + } + } + + return i +} diff --git a/project_euler/problem_3/problem3_test.go b/project_euler/problem_3/problem3_test.go new file mode 100644 index 000000000..d00379f8e --- /dev/null +++ b/project_euler/problem_3/problem3_test.go @@ -0,0 +1,40 @@ +package problem3 + +import "testing" + +// Tests +func TestProblem3_Func(t *testing.T) { + tests := []struct { + name string + input uint + want uint + }{ + { + name: "Testcase 1 - input 13195", + input: 13195, + want: 29, + }, + { + name: "Testcase 2 - input 600851475143", + input: 600851475143, + want: 6857, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem3(tt.input) + + if n != tt.want { + t.Errorf("Problem3() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem3(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem3(600851475143) + } +} diff --git a/project_euler/problem_4/problem4.go b/project_euler/problem_4/problem4.go new file mode 100644 index 000000000..b48d81e41 --- /dev/null +++ b/project_euler/problem_4/problem4.go @@ -0,0 +1,33 @@ +/** +* Problem 4 - Largest palindrome product +* @see {@link https://projecteuler.net/problem=4} +* +* A palindromic number reads the same both ways. +* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. +* Find the largest palindrome made from the product of two 3-digit numbers. +* +* @author ddaniel27 + */ +package problem4 + +import ( + "fmt" + + "github.com/TheAlgorithms/Go/strings/palindrome" +) + +func Problem4() uint { + max := uint(0) + + for i := 999; i >= 100; i-- { + for j := 999; j >= 100; j-- { + n := uint(i * j) + + if palindrome.IsPalindrome(fmt.Sprintf("%d", n)) && n > max { + max = n + } + } + } + + return max +} diff --git a/project_euler/problem_4/problem4_test.go b/project_euler/problem_4/problem4_test.go new file mode 100644 index 000000000..6b91fd2b0 --- /dev/null +++ b/project_euler/problem_4/problem4_test.go @@ -0,0 +1,33 @@ +package problem4 + +import "testing" + +// Tests +func TestProblem4_Func(t *testing.T) { + tests := []struct { + name string + want uint + }{ + { + name: "Testcase 1", + want: 906609, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem4() + + if n != tt.want { + t.Errorf("Problem4() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem4(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem4() + } +} diff --git a/project_euler/problem_5/problem5.go b/project_euler/problem_5/problem5.go new file mode 100644 index 000000000..dad415738 --- /dev/null +++ b/project_euler/problem_5/problem5.go @@ -0,0 +1,33 @@ +/** +* Problem 5 - Smallest multiple +* @see {@link https://projecteuler.net/problem=5} +* +* 2520 is the smallest number that can be divided by +* each of the numbers from 1 to 10 without any remainder. +* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? +* +* @author ddaniel27 + */ +package problem5 + +func Problem5(limit uint) uint { + n := limit * limit + + for { + if isDivisible(n, limit) { + return n + } + + n++ + } +} + +func isDivisible(n, limit uint) bool { + for i := uint(1); i <= limit; i++ { + if n%i != 0 { + return false + } + } + + return true +} diff --git a/project_euler/problem_5/problem5_test.go b/project_euler/problem_5/problem5_test.go new file mode 100644 index 000000000..43e7d8ec9 --- /dev/null +++ b/project_euler/problem_5/problem5_test.go @@ -0,0 +1,45 @@ +package problem5 + +import "testing" + +// Tests +func TestProblem5_Func(t *testing.T) { + tests := []struct { + name string + input uint + want uint + }{ + { + name: "Testcase 1 - input 10", + input: 10, + want: 2520, + }, + { + name: "Testcase 2 - input 20", + input: 20, + want: 232792560, + }, + { + name: "Testcase 3 - input 5", + input: 5, + want: 60, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem5(tt.input) + + if n != tt.want { + t.Errorf("Problem5() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem5(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem5(20) + } +} diff --git a/project_euler/problem_6/problem6.go b/project_euler/problem_6/problem6.go new file mode 100644 index 000000000..83287b87d --- /dev/null +++ b/project_euler/problem_6/problem6.go @@ -0,0 +1,33 @@ +/** +* Problem 6 - Sum square difference +* @see {@link https://projecteuler.net/problem=6} +* +* The sum of the squares of the first ten natural numbers is, +* 1^2 + 2^2 + ... + 10^2 = 385 +* +* The square of the sum of the first ten natural numbers is, +* (1 + 2 + ... + 10)^2 = 55^2 = 3025 +* +* Hence the difference between the sum of the squares of the first ten natural numbers +* and the square of the sum is 3025 − 385 = 2640. +* +* Find the difference between the sum of the squares of the first one hundred natural numbers +* and the square of the sum. +* +* @author ddaniel27 + */ +package problem6 + +func Problem6(n uint) uint { + sumOfSquares := uint(0) + squareOfSum := uint(0) + + for i := uint(1); i <= n; i++ { + sumOfSquares += i * i + squareOfSum += i + } + + squareOfSum *= squareOfSum + + return squareOfSum - sumOfSquares +} diff --git a/project_euler/problem_6/problem6_test.go b/project_euler/problem_6/problem6_test.go new file mode 100644 index 000000000..afa4e5fb7 --- /dev/null +++ b/project_euler/problem_6/problem6_test.go @@ -0,0 +1,40 @@ +package problem6 + +import "testing" + +// Tests +func TestProblem6_Func(t *testing.T) { + tests := []struct { + name string + input uint + want uint + }{ + { + name: "Testcase 1 - input 10", + input: 10, + want: 2640, + }, + { + name: "Testcase 2 - input 100", + input: 100, + want: 25164150, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem6(tt.input) + + if n != tt.want { + t.Errorf("Problem6() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem6(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem6(100) + } +} diff --git a/project_euler/problem_7/problem7.go b/project_euler/problem_7/problem7.go new file mode 100644 index 000000000..481844fed --- /dev/null +++ b/project_euler/problem_7/problem7.go @@ -0,0 +1,27 @@ +/** +* Problem 7 - 10001st prime +* @see {@link https://projecteuler.net/problem=7} +* +* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, +* we can see that the 6th prime is 13. +* +* What is the 10 001st prime number? +* +* @author ddaniel27 + */ +package problem7 + +import "github.com/TheAlgorithms/Go/math/prime" + +func Problem7(n uint) int64 { + count, i := uint(0), int64(1) + + for count < n { + i++ + if prime.OptimizedTrialDivision(i) { + count++ + } + } + + return i +} diff --git a/project_euler/problem_7/problem7_test.go b/project_euler/problem_7/problem7_test.go new file mode 100644 index 000000000..d2ab03e74 --- /dev/null +++ b/project_euler/problem_7/problem7_test.go @@ -0,0 +1,40 @@ +package problem7 + +import "testing" + +// Tests +func TestProblem7_Func(t *testing.T) { + tests := []struct { + name string + input uint + want int64 + }{ + { + name: "Testcase 1 - input 6", + input: 6, + want: 13, + }, + { + name: "Testcase 2 - input 10001", + input: 10001, + want: 104743, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem7(tt.input) + + if n != tt.want { + t.Errorf("Problem7() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem7(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem7(10001) + } +} diff --git a/project_euler/problem_8/problem8.go b/project_euler/problem_8/problem8.go new file mode 100644 index 000000000..0c800dcdb --- /dev/null +++ b/project_euler/problem_8/problem8.go @@ -0,0 +1,33 @@ +/** +* Problem 8 - Largest product in a series +* @see {@link https://projecteuler.net/problem=8} +* +* The four adjacent digits in the 1000-digit number that +* have the greatest product are 9 × 9 × 8 × 9 = 5832. +* Find the thirteen adjacent digits in the 1000-digit number +* that have the greatest product. What is the value of this product? +* +* @author ddaniel27 + */ +package problem8 + +const number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" + +func Problem8(window int) uint { + max := uint(0) + + for i := 0; i < len(number)-window; i++ { + product := uint(1) + + for j := 0; j < window; j++ { + n := uint(number[i+j] - '0') + product *= n + } + + if product > max { + max = product + } + } + + return max +} diff --git a/project_euler/problem_8/problem8_test.go b/project_euler/problem_8/problem8_test.go new file mode 100644 index 000000000..eecde4bc6 --- /dev/null +++ b/project_euler/problem_8/problem8_test.go @@ -0,0 +1,40 @@ +package problem8 + +import "testing" + +// Tests +func TestProblem8_Func(t *testing.T) { + tests := []struct { + name string + input int + want uint + }{ + { + name: "Testcase 1 - input 4", + input: 4, + want: 5832, + }, + { + name: "Testcase 2 - input 13", + input: 13, + want: 23514624000, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem8(tt.input) + + if n != tt.want { + t.Errorf("Problem8() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem8(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem8(13) + } +} diff --git a/project_euler/problem_9/problem9.go b/project_euler/problem_9/problem9.go new file mode 100644 index 000000000..0f784c022 --- /dev/null +++ b/project_euler/problem_9/problem9.go @@ -0,0 +1,29 @@ +/** +* Problem 9 - Special Pythagorean triplet +* @see {@link https://projecteuler.net/problem=9} +* +* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, +* a^2 + b^2 = c^2 +* +* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. +* +* There exists exactly one Pythagorean triplet for which a + b + c = 1000. +* Find the product abc. +* +* @author ddaniel27 + */ +package problem9 + +func Problem9() uint { + for a := uint(1); a < 1000; a++ { + for b := a + 1; b < 1000; b++ { + c := 1000 - a - b + + if a*a+b*b == c*c { + return a * b * c + } + } + } + + return 0 +} diff --git a/project_euler/problem_9/problem9_test.go b/project_euler/problem_9/problem9_test.go new file mode 100644 index 000000000..d75226103 --- /dev/null +++ b/project_euler/problem_9/problem9_test.go @@ -0,0 +1,33 @@ +package problem9 + +import "testing" + +// Tests +func TestProblem9_Func(t *testing.T) { + tests := []struct { + name string + want uint + }{ + { + name: "Testcase 1", + want: 31875000, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + n := Problem9() + + if n != tt.want { + t.Errorf("Problem9() = %v, want %v", n, tt.want) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem9(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = Problem9() + } +}