diff --git a/src/017/p017.exs b/src/017/p017.exs new file mode 100644 index 0000000..5b0c67a --- /dev/null +++ b/src/017/p017.exs @@ -0,0 +1,71 @@ +#!/usr/bin/env elixir +defmodule Problem017 do + + defp unit2string([x]) do + case x do + 1 -> "one" + 2 -> "two" + 3 -> "three" + 4 -> "four" + 5 -> "five" + 6 -> "six" + 7 -> "seven" + 8 -> "eight" + 9 -> "nine" + _ -> "" + end + end + + defp tens2string([1, units]) do + case units do + 0 -> "ten" + 1 -> "eleven" + 2 -> "twelve" + 3 -> "thirteen" + 4 -> "fourteen" + 5 -> "fifteen" + 6 -> "sixteen" + 7 -> "seventeen" + 8 -> "eighteen" + 9 -> "nineteen" + end + end + defp tens2string([tens|units]) do + case tens do + 2 -> "twenty" <> unit2string(units) + 3 -> "thirty" <> unit2string(units) + 4 -> "forty" <> unit2string(units) + 5 -> "fifty" <> unit2string(units) + 6 -> "sixty" <> unit2string(units) + 7 -> "seventy" <> unit2string(units) + 8 -> "eighty" <> unit2string(units) + 9 -> "ninety" <> unit2string(units) + 0 -> unit2string(units) + _ -> "" + end + end + + defp int2string([first|rest]) do + case Enum.count([first|rest]) do + 1 -> unit2string([first]) + 2 -> tens2string([first|rest]) + 3 -> + case rest do + [0,0] -> unit2string([first]) <> "hundred" + _ -> unit2string([first]) <> "hundredand" <> tens2string(rest) + end + _ -> "" + end + end + defp int2string(x), do: int2string(Integer.digits(x)) + + def solve do + 1..999 + |> Stream.map(&int2string/1) + |> Enum.reduce(&Kernel.<>/2) + |> Kernel.<>("onethousand") + |> String.length() + end +end + +IO.puts Problem017.solve diff --git a/src/019/p019.exs b/src/019/p019.exs new file mode 100644 index 0000000..8fc4e4a --- /dev/null +++ b/src/019/p019.exs @@ -0,0 +1,33 @@ +#!/usr/bin/env elixir +defmodule Problem019 do + def is_leap_year(year) when rem(year, 400) == 0, do: true + def is_leap_year(year) when rem(year, 100) == 0, do: false + def is_leap_year(year) when rem(year, 4) == 0, do: true + def is_leap_year(_), do: false + + def get_month_days(year) do + if is_leap_year(year) do + [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + else + [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + end + end + + def calculate_month_first_days([[], [_|month_tail]]), do: month_tail + def calculate_month_first_days([[delta_head|delta_tail], [month_head|month_tail]]) do + calculate_month_first_days([delta_tail, [rem(delta_head+month_head, 7) | [month_head|month_tail]]]) + end + + def solve do + # [1901, 2] means that 1901-01-01 was a tuesday + Stream.unfold([1901, 2], fn [year, weekday] -> {[year, weekday], + [year+1, rem(weekday+Enum.sum(get_month_days(year)), 7)]} end) + |> Stream.take_while(fn [year, _] -> year <= 2000 end) + |> Stream.map(fn [year, weekday] -> [get_month_days(year), [weekday]] end) + |> Stream.map(&calculate_month_first_days/1) + |> Stream.map(&(Enum.count(&1, fn(x) -> x == 0 end))) + |> Enum.sum() + end +end + +IO.puts Problem019.solve diff --git a/src/029/README.md b/src/029/README.md new file mode 100644 index 0000000..da0f647 --- /dev/null +++ b/src/029/README.md @@ -0,0 +1,14 @@ +https://projecteuler.net/problem=029 + +Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: + +- 22 = 4, 23 = 8, 24 = 16, 25 = 32 +- 32 = 9, 33 = 27, 34 = 81, 35 = 243 +- 42 = 16, 43 = 64, 44 = 256, 45 = 1024 +- 52 = 25, 53 = 125, 54 = 625, 55 = 3125 + +If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: + + 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 + +How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? diff --git a/src/029/p029.exs b/src/029/p029.exs new file mode 100644 index 0000000..9ba9f48 --- /dev/null +++ b/src/029/p029.exs @@ -0,0 +1,16 @@ +#!/usr/bin/env elixir +defmodule Problem029 do + defp generate_powers() do + for a <- 2..100, + b <- 2..100, + do: trunc(:math.pow(a, b)) + end + + def solve do + generate_powers() + |> Enum.uniq() + |> Enum.count() + end +end + +IO.puts Problem029.solve diff --git a/src/030/README.md b/src/030/README.md new file mode 100644 index 0000000..4866ada --- /dev/null +++ b/src/030/README.md @@ -0,0 +1,13 @@ +https://projecteuler.net/problem=030 + +Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: + +- 1634 = 14 + 64 + 34 + 44 +- 8208 = 84 + 24 + 04 + 84 +- 9474 = 94 + 44 + 74 + 44 + +As 1 = 14 is not a sum it is not included. + +The sum of these numbers is 1634 + 8208 + 9474 = 19316. + +Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. diff --git a/src/030/p030.exs b/src/030/p030.exs new file mode 100644 index 0000000..ef5b63b --- /dev/null +++ b/src/030/p030.exs @@ -0,0 +1,18 @@ +#!/usr/bin/env elixir +defmodule Problem030 do + defp digits_fifth_power(x) do + x + |> Integer.digits() + |> Enum.map(&(trunc(:math.pow(&1, 5)))) + |> Enum.sum() + end + + def solve do + Stream.iterate(2, &(&1+1)) + |> Enum.take_while(&(&1 < 200_000)) + |> Stream.filter(&(&1 == digits_fifth_power(&1))) + |> Enum.sum() + end +end + +IO.puts Problem030.solve diff --git a/src/034/README.md b/src/034/README.md new file mode 100644 index 0000000..7f70512 --- /dev/null +++ b/src/034/README.md @@ -0,0 +1,7 @@ +https://projecteuler.net/problem=034 + +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + +Find the sum of all numbers which are equal to the sum of the factorial of their digits. + +Note: as 1! = 1 and 2! = 2 are not sums they are not included. diff --git a/src/034/p034.exs b/src/034/p034.exs new file mode 100644 index 0000000..22337a8 --- /dev/null +++ b/src/034/p034.exs @@ -0,0 +1,22 @@ +#!/usr/bin/env elixir +defmodule Problem034 do + defp factorial(1), do: 1 + defp factorial(x) when is_integer(x) and x > 0, do: x*factorial(x-1) + + defp is_factorial_sum?(number) do + number == + number + |> Integer.digits() + |> Enum.map(&(factorial(&1))) + |> Enum.sum() + end + + def solve do + 3..40_585 # magic number! + |> Stream.filter(&(is_factorial_sum?(&1))) + |> Enum.to_list() + |> Enum.sum() + end +end + +IO.puts Problem034.solve diff --git a/src/048/README.md b/src/048/README.md index 89ac10b..7c03dac 100644 --- a/src/048/README.md +++ b/src/048/README.md @@ -1,4 +1,5 @@ https://projecteuler.net/problem=048 -The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. -Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. +The series, 11 + 22 + 33 + ... + 1010 = 10405071317. + +Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. diff --git a/src/048/p048.exs b/src/048/p048.exs new file mode 100644 index 0000000..d499953 --- /dev/null +++ b/src/048/p048.exs @@ -0,0 +1,29 @@ +#!/usr/bin/env elixir +defmodule Problem048 do + + defp pow_mod10000000000(_, 0), do: 1 + defp pow_mod10000000000(x, n) when rem(n, 2) != 0 do + x * pow_mod10000000000(x, n - 1) + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end + defp pow_mod10000000000(x, n) do + result = pow_mod10000000000(x, div(n, 2)) + result * result + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end + + def solve do + 1..1000 + |> Stream.map(&(pow_mod10000000000(&1, &1))) + |> Enum.sum() + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end +end + +IO.puts Problem048.solve diff --git a/src/055/README.md b/src/055/README.md new file mode 100644 index 0000000..22928cf --- /dev/null +++ b/src/055/README.md @@ -0,0 +1,19 @@ +https://projecteuler.net/problem=055 + +If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. + +Not all numbers produce palindromes so quickly. For example, + +349 + 943 = 1292, +1292 + 2921 = 4213 +4213 + 3124 = 7337 + +That is, 349 took three iterations to arrive at a palindrome. + +Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits). + +Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994. + +How many Lychrel numbers are there below ten-thousand? + +NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers. diff --git a/src/055/p055.exs b/src/055/p055.exs new file mode 100644 index 0000000..c8dd99b --- /dev/null +++ b/src/055/p055.exs @@ -0,0 +1,28 @@ +#!/usr/bin/env elixir +defmodule Problem055 do + defp reverse_number(x) do + x + |> Integer.to_string() + |> String.reverse() + |> String.to_integer() + end + + defp reverse_sum(x), do: x + reverse_number(x) + + defp is_palindrome(x), do: x == reverse_number(x) + + defp is_Lychrel_number(x) do + Stream.unfold(reverse_sum(x), fn n -> {n, reverse_sum(n)} end) + |> Stream.take(50) + |> Enum.any?(&is_palindrome/1) + |> Kernel.not + end + + def solve do + 1..10_000 + |> Stream.filter(&is_Lychrel_number/1) + |> Enum.count() + end +end + +IO.puts Problem055.solve diff --git a/src/089/README.md b/src/089/README.md new file mode 100644 index 0000000..7f3a7fe --- /dev/null +++ b/src/089/README.md @@ -0,0 +1,20 @@ +https://projecteuler.net/problem=089 + +For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number. + +For example, it would appear that there are at least six ways of writing the number sixteen: + + IIIIIIIIIIIIIIII + VIIIIIIIIIII + VVIIIIII + XIIIIII + VVVI + XVI + +However, according to the rules only `XIIIIII` and `XVI` are valid, and the last example is considered to be the most efficient, as it uses the least number of numerals. + +The 11K text file, [roman.txt](https://projecteuler.net/project/resources/p089_roman.txt) (right click and 'Save Link/Target As...'), contains one thousand numbers written in valid, but not necessarily minimal, Roman numerals; see [About... Roman Numerals](https://projecteuler.net/about=roman_numerals) for the definitive rules for this problem. + +Find the number of characters saved by writing each of these in their minimal form. + +Note: You can assume that all the Roman numerals in the file contain no more than four consecutive identical units. diff --git a/src/089/p089.exs b/src/089/p089.exs new file mode 100644 index 0000000..662cc8a --- /dev/null +++ b/src/089/p089.exs @@ -0,0 +1,109 @@ +#!/usr/bin/env elixir +defmodule Problem089 do + defp read_numbers_file() do + {:ok, numbers} = File.read("p089_roman.txt") + numbers + |> String.split("\n") + end + + defp roman_to_integer(roman_string) do + letter_values = %{ + "I" => 1, + "V" => 5, + "X" => 10, + "L" => 50, + "C" => 100, + "D" => 500, + "M" => 1000, + } + + roman_string + |> String.graphemes() + |> Enum.map(fn x -> elem(Map.fetch(letter_values, x), 1) end) + |> Enum.chunk_every(2, 1) + |> Enum.map(fn [a, b] when a >= b -> a + [a, _] -> -a + [a] -> a end) + |> Enum.sum() + end + + defp roman_units(number) do + case number do + 1 -> "I" + 2 -> "II" + 3 -> "III" + 4 -> "IV" + 5 -> "V" + 6 -> "VI" + 7 -> "VII" + 8 -> "VIII" + 9 -> "IX" + _ -> "" + end + end + + defp roman_tens(number) do + case number do + 1 -> "X" + 2 -> "XX" + 3 -> "XXX" + 4 -> "XL" + 5 -> "L" + 6 -> "LX" + 7 -> "LXX" + 8 -> "LXXX" + 9 -> "XC" + _ -> "" + end + end + + defp roman_hundreds(number) do + case number do + 1 -> "C" + 2 -> "CC" + 3 -> "CCC" + 4 -> "CD" + 5 -> "D" + 6 -> "DC" + 7 -> "DCC" + 8 -> "DCCC" + 9 -> "CM" + _ -> "" + end + end + + defp roman_thousands(number), do: String.duplicate("M", number) + + defp integer_to_roman([head|tail], roman_string) do + case Enum.count(tail) do + 0 -> roman_string <> roman_units(head) + 1 -> integer_to_roman(tail, roman_string <> roman_tens(head)) + 2 -> integer_to_roman(tail, roman_string <> roman_hundreds(head)) + 3 -> integer_to_roman(tail, roman_string <> roman_thousands(head)) + _ -> :error + end + end + defp integer_to_roman(number), do: integer_to_roman(Integer.digits(number), "") + + def solve do + numbers = read_numbers_file() + + original_file_len = + numbers + |> Enum.join() + |> String.length() + + new_file_len = + numbers + |> Enum.to_list() + |> Enum.map(&roman_to_integer/1) + |> Enum.map(&integer_to_roman/1) + |> Enum.to_list() + |> Enum.join() + |> String.length() + + original_file_len - new_file_len + end +end + +IO.puts Problem089.solve diff --git a/src/089/p089_roman.txt b/src/089/p089_roman.txt new file mode 100644 index 0000000..50651c3 --- /dev/null +++ b/src/089/p089_roman.txt @@ -0,0 +1,1000 @@ +MMMMDCLXXII +MMDCCCLXXXIII +MMMDLXVIIII +MMMMDXCV +DCCCLXXII +MMCCCVI +MMMCDLXXXVII +MMMMCCXXI +MMMCCXX +MMMMDCCCLXXIII +MMMCCXXXVII +MMCCCLXXXXIX +MDCCCXXIIII +MMCXCVI +CCXCVIII +MMMCCCXXXII +MDCCXXX +MMMDCCCL +MMMMCCLXXXVI +MMDCCCXCVI +MMMDCII +MMMCCXII +MMMMDCCCCI +MMDCCCXCII +MDCXX +CMLXXXVII +MMMXXI +MMMMCCCXIV +MLXXII +MCCLXXVIIII +MMMMCCXXXXI +MMDCCCLXXII +MMMMXXXI +MMMDCCLXXX +MMDCCCLXXIX +MMMMLXXXV +MCXXI +MDCCCXXXVII +MMCCCLXVII +MCDXXXV +CCXXXIII +CMXX +MMMCLXIV +MCCCLXXXVI +DCCCXCVIII +MMMDCCCCXXXIV +CDXVIIII +MMCCXXXV +MDCCCXXXII +MMMMD +MMDCCLXIX +MMMMCCCLXXXXVI +MMDCCXLII +MMMDCCCVIIII +DCCLXXXIIII +MDCCCCXXXII +MMCXXVII +DCCCXXX +CCLXIX +MMMXI +MMMMCMLXXXXVIII +MMMMDLXXXVII +MMMMDCCCLX +MMCCLIV +CMIX +MMDCCCLXXXIIII +CLXXXII +MMCCCCXXXXV +MMMMDLXXXVIIII +MMMDCCCXXI +MMDCCCCLXXVI +MCCCCLXX +MMCDLVIIII +MMMDCCCLIX +MMMMCCCCXIX +MMMDCCCLXXV +XXXI +CDLXXXIII +MMMCXV +MMDCCLXIII +MMDXXX +MMMMCCCLVII +MMMDCI +MMMMCDLXXXIIII +MMMMCCCXVI +CCCLXXXVIII +MMMMCML +MMMMXXIV +MMMCCCCXXX +DCCX +MMMCCLX +MMDXXXIII +CCCLXIII +MMDCCXIII +MMMCCCXLIV +CLXXXXI +CXVI +MMMMCXXXIII +CLXX +DCCCXVIII +MLXVII +DLXXXX +MMDXXI +MMMMDLXXXXVIII +MXXII +LXI +DCCCCXLIII +MMMMDV +MMMMXXXIV +MDCCCLVIII +MMMCCLXXII +MMMMDCCXXXVI +MMMMLXXXIX +MDCCCLXXXI +MMMMDCCCXV +MMMMCCCCXI +MMMMCCCLIII +MDCCCLXXI +MMCCCCXI +MLXV +MMCDLXII +MMMMDXXXXII +MMMMDCCCXL +MMMMCMLVI +CCLXXXIV +MMMDCCLXXXVI +MMCLII +MMMCCCCXV +MMLXXXIII +MMMV +MMMV +DCCLXII +MMDCCCCXVI +MMDCXLVIII +CCLIIII +CCCXXV +MMDCCLXXXVIIII +MMMMDCLXXVIII +MMMMDCCCXCI +MMMMCCCXX +MMCCXLV +MMMDCCCLXIX +MMCCLXIIII +MMMDCCCXLIX +MMMMCCCLXIX +CMLXXXXI +MCMLXXXIX +MMCDLXI +MMDCLXXVIII +MMMMDCCLXI +MCDXXV +DL +CCCLXXII +MXVIIII +MCCCCLXVIII +CIII +MMMDCCLXXIIII +MMMDVIII +MMMMCCCLXXXXVII +MMDXXVII +MMDCCLXXXXV +MMMMCXLVI +MMMDCCLXXXII +MMMDXXXVI +MCXXII +CLI +DCLXXXIX +MMMCLI +MDCLXIII +MMMMDCCXCVII +MMCCCLXXXV +MMMDCXXVIII +MMMCDLX +MMMCMLII +MMMIV +MMMMDCCCLVIII +MMMDLXXXVIII +MCXXIV +MMMMLXXVI +CLXXIX +MMMCCCCXXVIIII +DCCLXXXV +MMMDCCCVI +LI +CLXXXVI +MMMMCCCLXXVI +MCCCLXVI +CCXXXIX +MMDXXXXI +MMDCCCXLI +DCCCLXXXVIII +MMMMDCCCIV +MDCCCCXV +MMCMVI +MMMMCMLXXXXV +MMDCCLVI +MMMMCCXLVIII +DCCCCIIII +MMCCCCIII +MMMDCCLXXXVIIII +MDCCCLXXXXV +DVII +MMMV +DCXXV +MMDCCCXCV +DCVIII +MMCDLXVI +MCXXVIII +MDCCXCVIII +MMDCLX +MMMDCCLXIV +MMCDLXXVII +MMDLXXXIIII +MMMMCCCXXII +MMMDCCCXLIIII +DCCCCLXVII +MMMCLXXXXIII +MCCXV +MMMMDCXI +MMMMDCLXXXXV +MMMCCCLII +MMCMIX +MMDCCXXV +MMDLXXXVI +MMMMDCXXVIIII +DCCCCXXXVIIII +MMCCXXXIIII +MMDCCLXXVIII +MDCCLXVIIII +MMCCLXXXV +MMMMDCCCLXXXVIII +MMCMXCI +MDXLII +MMMMDCCXIV +MMMMLI +DXXXXIII +MMDCCXI +MMMMCCLXXXIII +MMMDCCCLXXIII +MDCLVII +MMCD +MCCCXXVII +MMMMDCCIIII +MMMDCCXLVI +MMMCLXXXVII +MMMCCVIIII +MCCCCLXXIX +DL +DCCCLXXVI +MMDXCI +MMMMDCCCCXXXVI +MMCII +MMMDCCCXXXXV +MMMCDXLV +MMDCXXXXIV +MMD +MDCCCLXXXX +MMDCXLIII +MMCCXXXII +MMDCXXXXVIIII +DCCCLXXI +MDXCVIIII +MMMMCCLXXVIII +MDCLVIIII +MMMCCCLXXXIX +MDCLXXXV +MDLVIII +MMMMCCVII +MMMMDCXIV +MMMCCCLXIIII +MMIIII +MMMMCCCLXXIII +CCIII +MMMCCLV +MMMDXIII +MMMCCCXC +MMMDCCCXXI +MMMMCCCCXXXII +CCCLVI +MMMCCCLXXXVI +MXVIIII +MMMCCCCXIIII +CLXVII +MMMCCLXX +CCCCLXIV +MMXXXXII +MMMMCCLXXXX +MXL +CCXVI +CCCCLVIIII +MMCCCII +MCCCLVIII +MMMMCCCX +MCDLXXXXIV +MDCCCXIII +MMDCCCXL +MMMMCCCXXIII +DXXXIV +CVI +MMMMDCLXXX +DCCCVII +MMCMLXIIII +MMMDCCCXXXIII +DCCC +MDIII +MMCCCLXVI +MMMCCCCLXXI +MMDCCCCXVIII +CCXXXVII +CCCXXV +MDCCCXII +MMMCMV +MMMMCMXV +MMMMDCXCI +DXXI +MMCCXLVIIII +MMMMCMLII +MDLXXX +MMDCLXVI +CXXI +MMMDCCCLIIII +MMMCXXI +MCCIII +MMDCXXXXI +CCXCII +MMMMDXXXV +MMMCCCLXV +MMMMDLXV +MMMCCCCXXXII +MMMCCCVIII +DCCCCLXXXXII +MMCLXIV +MMMMCXI +MLXXXXVII +MMMCDXXXVIII +MDXXII +MLV +MMMMDLXVI +MMMCXII +XXXIII +MMMMDCCCXXVI +MMMLXVIIII +MMMLX +MMMCDLXVII +MDCCCLVII +MMCXXXVII +MDCCCCXXX +MMDCCCLXIII +MMMMDCXLIX +MMMMCMXLVIII +DCCCLXXVIIII +MDCCCLIII +MMMCMLXI +MMMMCCLXI +MMDCCCLIII +MMMDCCCVI +MMDXXXXIX +MMCLXXXXV +MMDXXX +MMMXIII +DCLXXIX +DCCLXII +MMMMDCCLXVIII +MDCCXXXXIII +CCXXXII +MMMMDCXXV +MMMCCCXXVIII +MDCVIII +MMMCLXXXXIIII +CLXXXI +MDCCCCXXXIII +MMMMDCXXX +MMMDCXXIV +MMMCCXXXVII +MCCCXXXXIIII +CXVIII +MMDCCCCIV +MMMMCDLXXV +MMMDLXIV +MDXCIII +MCCLXXXI +MMMDCCCXXIV +MCXLIII +MMMDCCCI +MCCLXXX +CCXV +MMDCCLXXI +MMDLXXXIII +MMMMDCXVII +MMMCMLXV +MCLXVIII +MMMMCCLXXVI +MMMDCCLXVIIII +MMMMDCCCIX +DLXXXXIX +DCCCXXII +MMMMIII +MMMMCCCLXXVI +DCCCXCIII +DXXXI +MXXXIIII +CCXII +MMMDCCLXXXIIII +MMMCXX +MMMCMXXVII +DCCCXXXX +MMCDXXXVIIII +MMMMDCCXVIII +LV +MMMDCCCCVI +MCCCII +MMCMLXVIIII +MDCCXI +MMMMDLXVII +MMCCCCLXI +MMDCCV +MMMCCCXXXIIII +MMMMDI +MMMDCCCXCV +MMDCCLXXXXI +MMMDXXVI +MMMDCCCLVI +MMDCXXX +MCCCVII +MMMMCCCLXII +MMMMXXV +MMCMXXV +MMLVI +MMDXXX +MMMMCVII +MDC +MCCIII +MMMMDCC +MMCCLXXV +MMDCCCXXXXVI +MMMMCCCLXV +CDXIIII +MLXIIII +CCV +MMMCMXXXI +CCCCLXVI +MDXXXII +MMMMCCCLVIII +MMV +MMMCLII +MCMLI +MMDCCXX +MMMMCCCCXXXVI +MCCLXXXI +MMMCMVI +DCCXXX +MMMMCCCLXV +DCCCXI +MMMMDCCCXIV +CCCXXI +MMDLXXV +CCCCLXXXX +MCCCLXXXXII +MMDCIX +DCCXLIIII +DXIV +MMMMCLII +CDLXI +MMMCXXVII +MMMMDCCCCLXIII +MMMDCLIIII +MCCCCXXXXII +MMCCCLX +CCCCLIII +MDCCLXXVI +MCMXXIII +MMMMDLXXVIII +MMDCCCCLX +MMMCCCLXXXX +MMMCDXXVI +MMMDLVIII +CCCLXI +MMMMDCXXII +MMDCCCXXI +MMDCCXIII +MMMMCLXXXVI +MDCCCCXXVI +MDV +MMDCCCCLXXVI +MMMMCCXXXVII +MMMDCCLXXVIIII +MMMCCCCLXVII +DCCXLI +MMCLXXXVIII +MCCXXXVI +MMDCXLVIII +MMMMCXXXII +MMMMDCCLXVI +MMMMCMLI +MMMMCLXV +MMMMDCCCXCIV +MCCLXXVII +LXXVIIII +DCCLII +MMMCCCXCVI +MMMCLV +MMDCCCXXXXVIII +DCCCXV +MXC +MMDCCLXXXXVII +MMMMCML +MMDCCCLXXVIII +DXXI +MCCCXLI +DCLXXXXI +MMCCCLXXXXVIII +MDCCCCLXXVIII +MMMMDXXV +MMMDCXXXVI +MMMCMXCVII +MMXVIIII +MMMDCCLXXIV +MMMCXXV +DXXXVIII +MMMMCLXVI +MDXII +MMCCCLXX +CCLXXI +DXIV +MMMCLIII +DLII +MMMCCCXLIX +MMCCCCXXVI +MMDCXLIII +MXXXXII +CCCLXXXV +MDCLXXVI +MDCXII +MMMCCCLXXXIII +MMDCCCCLXXXII +MMMMCCCLXXXV +MMDCXXI +DCCCXXX +MMMDCCCCLII +MMMDCCXXII +MMMMCDXCVIII +MMMCCLXVIIII +MMXXV +MMMMCDXIX +MMMMCCCX +MMMCCCCLXVI +MMMMDCLXXVIIII +MMMMDCXXXXIV +MMMCMXII +MMMMXXXIII +MMMMDLXXXII +DCCCLIV +MDXVIIII +MMMCLXXXXV +CCCCXX +MMDIX +MMCMLXXXVIII +DCCXLIII +DCCLX +D +MCCCVII +MMMMCCCLXXXIII +MDCCCLXXIIII +MMMDCCCCLXXXVII +MMMMCCCVII +MMMDCCLXXXXVI +CDXXXIV +MCCLXVIII +MMMMDLX +MMMMDXII +MMMMCCCCLIIII +MCMLXXXXIII +MMMMDCCCIII +MMDCLXXXIII +MDCCCXXXXIV +XXXXVII +MMMDCCCXXXII +MMMDCCCXLII +MCXXXV +MDCXXVIIII +MMMCXXXXIIII +MMMMCDXVII +MMMDXXIII +MMMMCCCCLXI +DCLXXXXVIIII +LXXXXI +CXXXIII +MCDX +MCCLVII +MDCXXXXII +MMMCXXIV +MMMMLXXXX +MMDCCCCXLV +MLXXX +MMDCCCCLX +MCDLIII +MMMCCCLXVII +MMMMCCCLXXIV +MMMDCVIII +DCCCCXXIII +MMXCI +MMDCCIV +MMMMDCCCXXXIV +CCCLXXI +MCCLXXXII +MCMIII +CCXXXI +DCCXXXVIII +MMMMDCCXLVIIII +MMMMCMXXXV +DCCCLXXV +DCCXCI +MMMMDVII +MMMMDCCCLXVIIII +CCCXCV +MMMMDCCXX +MCCCCII +MMMCCCXC +MMMCCCII +MMDCCLXXVII +MMDCLIIII +CCXLIII +MMMDCXVIII +MMMCCCIX +MCXV +MMCCXXV +MLXXIIII +MDCCXXVI +MMMCCCXX +MMDLXX +MMCCCCVI +MMDCCXX +MMMMDCCCCXCV +MDCCCXXXII +MMMMDCCCCXXXX +XCIV +MMCCCCLX +MMXVII +MLXXI +MMMDXXVIII +MDCCCCII +MMMCMLVII +MMCLXXXXVIII +MDCCCCLV +MCCCCLXXIIII +MCCCLII +MCDXLVI +MMMMDXVIII +DCCLXXXIX +MMMDCCLXIV +MDCCCCXLIII +CLXXXXV +MMMMCCXXXVI +MMMDCCCXXI +MMMMCDLXXVII +MCDLIII +MMCCXLVI +DCCCLV +MCDLXX +DCLXXVIII +MMDCXXXIX +MMMMDCLX +MMDCCLI +MMCXXXV +MMMCCXII +MMMMCMLXII +MMMMCCV +MCCCCLXIX +MMMMCCIII +CLXVII +MCCCLXXXXIIII +MMMMDCVIII +MMDCCCLXI +MMLXXIX +CMLXIX +MMDCCCXLVIIII +DCLXII +MMMCCCXLVII +MDCCCXXXV +MMMMDCCXCVI +DCXXX +XXVI +MMLXIX +MMCXI +DCXXXVII +MMMMCCCXXXXVIII +MMMMDCLXI +MMMMDCLXXIIII +MMMMVIII +MMMMDCCCLXII +MDCXCI +MMCCCXXIIII +CCCCXXXXV +MMDCCCXXI +MCVI +MMDCCLXVIII +MMMMCXL +MLXVIII +CMXXVII +CCCLV +MDCCLXXXIX +MMMCCCCLXV +MMDCCLXII +MDLXVI +MMMCCCXVIII +MMMMCCLXXXI +MMCXXVII +MMDCCCLXVIII +MMMCXCII +MMMMDCLVIII +MMMMDCCCXXXXII +MMDCCCCLXXXXVI +MDCCXL +MDCCLVII +MMMMDCCCLXXXVI +DCCXXXIII +MMMMDCCCCLXXXV +MMCCXXXXVIII +MMMCCLXXVIII +MMMDCLXXVIII +DCCCI +MMMMLXXXXVIIII +MMMCCCCLXXII +MMCLXXXVII +CCLXVI +MCDXLIII +MMCXXVIII +MDXIV +CCCXCVIII +CLXXVIII +MMCXXXXVIIII +MMMDCLXXXIV +CMLVIII +MCDLIX +MMMMDCCCXXXII +MMMMDCXXXIIII +MDCXXI +MMMDCXLV +MCLXXVIII +MCDXXII +IV +MCDLXXXXIII +MMMMDCCLXV +CCLI +MMMMDCCCXXXVIII +DCLXII +MCCCLXVII +MMMMDCCCXXXVI +MMDCCXLI +MLXI +MMMCDLXVIII +MCCCCXCIII +XXXIII +MMMDCLXIII +MMMMDCL +DCCCXXXXIIII +MMDLVII +DXXXVII +MCCCCXXIIII +MCVII +MMMMDCCXL +MMMMCXXXXIIII +MCCCCXXIV +MMCLXVIII +MMXCIII +MDCCLXXX +MCCCLIIII +MMDCLXXI +MXI +MCMLIV +MMMCCIIII +DCCLXXXVIIII +MDCLIV +MMMDCXIX +CMLXXXI +DCCLXXXVII +XXV +MMMXXXVI +MDVIIII +CLXIII +MMMCDLVIIII +MMCCCCVII +MMMLXX +MXXXXII +MMMMCCCLXVIII +MMDCCCXXVIII +MMMMDCXXXXI +MMMMDCCCXXXXV +MMMXV +MMMMCCXVIIII +MMDCCXIIII +MMMXXVII +MDCCLVIIII +MMCXXIIII +MCCCLXXIV +DCLVIII +MMMLVII +MMMCXLV +MMXCVII +MMMCCCLXXXVII +MMMMCCXXII +DXII +MMMDLV +MCCCLXXVIII +MMMCLIIII +MMMMCLXXXX +MMMCLXXXIIII +MDCXXIII +MMMMCCXVI +MMMMDLXXXIII +MMMDXXXXIII +MMMMCCCCLV +MMMDLXXXI +MMMCCLXXVI +MMMMXX +MMMMDLVI +MCCCCLXXX +MMMXXII +MMXXII +MMDCCCCXXXI +MMMDXXV +MMMDCLXXXVIIII +MMMDLXXXXVII +MDLXIIII +CMXC +MMMXXXVIII +MDLXXXVIII +MCCCLXXVI +MMCDLIX +MMDCCCXVIII +MDCCCXXXXVI +MMMMCMIV +MMMMDCIIII +MMCCXXXV +XXXXVI +MMMMCCXVII +MMCCXXIV +MCMLVIIII +MLXXXIX +MMMMLXXXIX +CLXXXXIX +MMMDCCCCLVIII +MMMMCCLXXIII +MCCCC +DCCCLIX +MMMCCCLXXXII +MMMCCLXVIIII +MCLXXXV +CDLXXXVII +DCVI +MMX +MMCCXIII +MMMMDCXX +MMMMXXVIII +DCCCLXII +MMMMCCCXLIII +MMMMCLXV +DXCI +MMMMCLXXX +MMMDCCXXXXI +MMMMXXXXVI +DCLX +MMMCCCXI +MCCLXXX +MMCDLXXII +DCCLXXI +MMMCCCXXXVI +MCCCCLXXXVIIII +CDLVIII +DCCLVI +MMMMDCXXXVIII +MMCCCLXXXIII +MMMMDCCLXXV +MMMXXXVI +CCCLXXXXIX +CV +CCCCXIII +CCCCXVI +MDCCCLXXXIIII +MMDCCLXXXII +MMMMCCCCLXXXI +MXXV +MMCCCLXXVIIII +MMMCCXII +MMMMCCXXXIII +MMCCCLXXXVI +MMMDCCCLVIIII +MCCXXXVII +MDCLXXV +XXXV +MMDLI +MMMCCXXX +MMMMCXXXXV +CCCCLIX +MMMMDCCCLXXIII +MMCCCXVII +DCCCXVI +MMMCCCXXXXV +MDCCCCXCV +CLXXXI +MMMMDCCLXX +MMMDCCCIII +MMCLXXVII +MMMDCCXXIX +MMDCCCXCIIII +MMMCDXXIIII +MMMMXXVIII +MMMMDCCCCLXVIII +MDCCCXX +MMMMCDXXI +MMMMDLXXXIX +CCXVI +MDVIII +MMCCLXXI +MMMDCCCLXXI +MMMCCCLXXVI +MMCCLXI +MMMMDCCCXXXIV +DLXXXVI +MMMMDXXXII +MMMXXIIII +MMMMCDIV +MMMMCCCXLVIII +MMMMCXXXVIII +MMMCCCLXVI +MDCCXVIII +MMCXX +CCCLIX +MMMMDCCLXXII +MDCCCLXXV +MMMMDCCCXXIV +DCCCXXXXVIII +MMMDCCCCXXXVIIII +MMMMCCXXXV +MDCLXXXIII +MMCCLXXXIV +MCLXXXXIIII +DXXXXIII +MCCCXXXXVIII +MMCLXXIX +MMMMCCLXIV +MXXII +MMMCXIX +MDCXXXVII +MMDCCVI +MCLXXXXVIII +MMMCXVI +MCCCLX +MMMCDX +CCLXVIIII +MMMCCLX +MCXXVIII +LXXXII +MCCCCLXXXI +MMMI +MMMCCCLXIV +MMMCCCXXVIIII +CXXXVIII +MMCCCXX +MMMCCXXVIIII +MCCLXVI +MMMCCCCXXXXVI +MMDCCXCIX +MCMLXXI +MMCCLXVIII +CDLXXXXIII +MMMMDCCXXII +MMMMDCCLXXXVII +MMMDCCLIV +MMCCLXIII +MDXXXVII +DCCXXXIIII +MCII +MMMDCCCLXXI +MMMLXXIII +MDCCCLIII +MMXXXVIII +MDCCXVIIII +MDCCCCXXXVII +MMCCCXVI +MCMXXII +MMMCCCLVIII +MMMMDCCCXX +MCXXIII +MMMDLXI +MMMMDXXII +MDCCCX +MMDXCVIIII +MMMDCCCCVIII +MMMMDCCCCXXXXVI +MMDCCCXXXV +MMCXCIV +MCMLXXXXIII +MMMCCCLXXVI +MMMMDCLXXXV +CMLXIX +DCXCII +MMXXVIII +MMMMCCCXXX +XXXXVIIII \ No newline at end of file diff --git a/src/097/p097.exs b/src/097/p097.exs new file mode 100644 index 0000000..4a7d5b4 --- /dev/null +++ b/src/097/p097.exs @@ -0,0 +1,29 @@ +#!/usr/bin/env elixir +defmodule Problem097 do + + defp pow_mod10000000000(_, 0), do: 1 + defp pow_mod10000000000(x, n) when rem(n, 2) != 0 do + x * pow_mod10000000000(x, n - 1) + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end + defp pow_mod10000000000(x, n) do + result = pow_mod10000000000(x, div(n, 2)) + result * result + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end + + def solve do + pow_mod10000000000(2, 7830457) + |> Kernel.*(28433) + |> Kernel.+(1) + |> Integer.digits() + |> Enum.take(-10) + |> Integer.undigits() + end +end + +IO.puts Problem097.solve