Euler Problems in OCaml
Thursday, 19 March 2009 15:23

The Euler Project provides a set of exercises, especially suited for novices in programming languages. Should you wish to take part in the competition, register at their website; and if you don't mind the spoiler, have a look at the solutions in Haskell. On this page I accumulate the algorithms written in OCaml. Contributions and questions are welcome at ocaml(at)christiankissig.de .

Note The authors of the euler project seem to suspect that some members use solution lists to increase their grades. I have therefore decided to discontinue publishing of plain solutions for now. If you get stuck with any problem in the list, which I indicate to have solved, feel free to ask me.

  1. Add all the natural numbers below one thousand multiples of that are 3 or 5.
    [Solution, OCaml]
  2. Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
    [Solution, OCaml]
  3. Find the largest prime factor of a composite number.
    [Solution, OCaml]
  4. Find the largest palindrome made from the product of two 3-digit numbers.
    [Solution, OCaml]
  5. What is the smallest number divisible by each of the numbers 1 to 20?
    [Solution, OCaml]
  6. What is the difference between the sum of the squares and the square of the sums?
    [Solution, OCaml]
  7. Find the 10001st prime.
    [Solution, OCaml]
  8. Discover the largest product of five consecutive digits in the 1000-digit number.
    [Solution, OCaml]
  9. Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
    [Solution, OCaml]
  10. Calculate the sum of all the primes below two million.
    [Solution, OCaml]
  11. What is the greatest product of four numbers on the same straight line in the 20 by 20 grid?
    [Solution, OCaml]
  12. What is the value of the first triangle number to have over five hundred divisors.
    [solved]
  13. Find the first ten digits of the sum of one-hundred 50-digit numbers.
    [Solution, OCaml]
  14. Find the longest sequence using a starting number under one million.
    [solved]
  15. Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?
    [solved]
  16. What is the sum of the digits of the number 21000?
    [Solution, OCaml]
  17. How many letters would be needed to write all the numbers in words from 1 to 1000?
    [solved]
  18. Find the maximum sum travelling from the top of the triangle to the base.
    [solved]
  19. How many Sundays fell on the first of the month during the twentieth century?
    [Solution, OCaml]
  20. Find the sum of digits in 100!
    [Solution, OCaml]
  21. Evaluate the sum of all amicable pairs under 10000.
    [Solution, OCaml]
  22. What is the total of all the name scores in the file of first names?
  23. Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
    [Solution, OCaml]
  24. What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
    [solved]
  25. What is the first term in the Fibonacci sequence to contain 1000 digits?
    [Solution, OCaml]
  26. Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
    [solved]
  27. Find a quadratic formula that produces the maximum number of primes for consecutive values of n.
    [solved]
  28. What is the sum of both diagonals in a 1001 by 1001 spiral?
  29. How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
    [solved]
  30. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
    [solved]
  31. Investigating combinations of English currency denominations.
    [Solution, OCaml]
  32. Find the sum of all numbers that can be written as pandigital products.
  33. Discover all the fractions with an unorthodox cancelling method.
  34. Find the sum of all numbers which are equal to the sum of the factorial of their digits.
  35. How many circular primes are there below one million?
    [solved]
  36. Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.
    [Solution, OCaml]
  37. Find the sum of all eleven primes that are both truncatable from left to right and right to left.
    [Solution, OCaml]
  38. What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ...?
  39. If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p ≤ 1000, has the most solutions?
  40. Finding the nth digit of the fractional part of the irrational number.
  41. What is the largest n-digit pandigital prime that exists?
  42. How many triangle words does the list of common English words contain?
  43. Find the sum of all pandigital numbers with an unusual sub-string divisibility property.
  44. Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
  45. After 40755, what is the next triangle number that is also pentagonal and hexagonal?
    [solved]
  46. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
    [Solution, OCaml]
  47. Find the first four consecutive integers to have four distinct primes factors.
  48. Find the last ten digits of 11 + 22 + ... + 10001000.
  49. Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.
    [Solution, OCaml]
  50. Which prime, below one-million, can be written as the sum of the most consecutive primes?
  51. Find the smallest prime which, by changing the same part of the number, can form eight different primes.
  52. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits in some order.
    [Solution, OCaml]
  53. How many values of C(n,r), for 1 ≤ n ≤ 100, exceed one-million?
    [Solution, OCaml]
  54. How many hands did player one win in the game of poker?
  55. How many Lychrel numbers are there below ten-thousand?
    [solved]
  56. Considering natural numbers of the form, ab, finding the maximum digital sum.
    [solved]
  57. Investigate the expansion of the continued fraction for the square root of two.
  58. Investigate the number of primes that lie on the diagonals of the spiral grid.
  59. Using a brute force attack, can you decrypt the cipher using XOR encryption?
  60. Find a set of five primes for which any two primes concatenate to produce another prime.
  61. Find the sum of the only set of six 4-digit figurate numbers with a cyclic property.
  62. Find the smallest cube for which exactly five permutations of its digits are cube.
  63. How many n-digit positive integers exist which are also an nth power?
    [Solution, OCaml]
  64. How many continued fractions for N ≤ 10000 have an odd period?
  65. Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.
  66. Investigate the Diophantine equation x2 − Dy2 = 1.
  67. Using an efficient algorithm find the maximal sum in the triangle?
  68. What is the maximum 16-digit string for a "magic" 5-gon ring?
  69. Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum.
  70. Investigate values of n for which φ(n) is a permutation of n.
  71. Listing reduced proper fractions in ascending order of size.
  72. How many elements would be contained in the set of reduced proper fractions for d ≤ 1,000,000?
  73. How many fractions lie between 1/3 and 1/2 in a sorted set of reduced proper fractions?
    [Solution, OCaml]
  74. Determine the number of factorial chains that contain exactly sixty non-repeating terms.
    [Solution, OCaml]
  75. Find the number of different lengths of wire can that can form a right angle triangle in only one way.
  76. How many different ways can one hundred be written as a sum of at least two positive integers?
  77. What is the first value which can be written as the sum of primes in over five thousand different ways?
  78. Investigating the number of ways in which coins can be separated into piles.
  79. By analysing a user's login attempts, can you determine the secret numeric passcode?
  80. Calculating the digital sum of the decimal digits of irrational square roots.
  81. Find the minimal path sum from the top left to the bottom right by moving right and down.
  82. Find the minimal path sum from the left column to the right column.
  83. Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down.
  84. In the game, Monopoly, find the three most popular squares when using two 4-sided dice.
  85. Investigating the number of rectangles in a rectangular grid.
  86. Exploring the shortest path from one corner of a cuboid to another.
  87. Investigating numbers that can be expressed as the sum of a prime square, cube, and fourth power?
  88. Exploring minimal product-sum numbers for sets of different sizes.
  89. Develop a method to express Roman numerals in minimal form.
  90. An unexpected way of using two cubes to make a square.
  91. Find the number of right angle triangles in the quadrant.
  92. Investigating a square digits number chain with a surprising property.
  93. Using four distinct digits and the rules of arithmetic, find the longest sequence of target numbers.
  94. Investigating almost equilateral triangles with integral sides and area.
  95. Find the smallest member of the longest amicable chain with no element exceeding one million.
  96. Devise an algorithm for solving Su Doku puzzles.
  97. Find the last ten digits of the non-Mersenne prime: 28433 × 27830457 + 1.
  98. Investigating words, and their anagrams, which can represent square numbers.
  99. Which base/exponent pair in the file has the greatest numerical value?
  100. Finding the number of blue discs for which there is 50% chance of taking two blue.
  101. Investigate the optimum polynomial function to model the first k terms of a given sequence.
  102. For how many triangles in the text file does the interior contain the origin?
  103. Investigating sets with a special subset sum property.
  104. Finding Fibonacci numbers for which the first and last nine digits are pandigital.
  105. Find the sum of the special sum sets in the file.
  106. Find the minimum number of comparisons needed to identify special sum sets.
  107. Determining the most efficient way to connect the network.
  108. Solving the Diophantine equation 1/x + 1/y = 1/n.
  109. How many distinct ways can a player checkout in the game of darts with a score of less than 100?
  110. Find an efficient algorithm to analyse the number of solutions of the equation 1/x + 1/y = 1/n.
  111. Search for 10-digit primes containing the maximum number of repeated digits.
  112. Investigating the density of "bouncy" numbers.
  113. How many numbers below a googol (10100) are not "bouncy"?
  114. Investigating the number of ways to fill a row with separated blocks that are at least three units long.
  115. Finding a generalisation for the number of ways to fill a row with separated blocks.
  116. Investigating the number of ways of replacing square tiles with one of three coloured tiles.
  117. Investigating the number of ways of tiling a row using different-sized tiles.
  118. Exploring the number of ways in which sets containing prime elements can be made.
  119. Investigating the numbers which are equal to sum of their digits raised to some power.
  120. Finding the maximum remainder when (a − 1)n + (a + 1)n is divided by a2.
  121. Investigate the game of chance involving coloured discs.
  122. Finding the most efficient exponentiation method.
  123. Determining the remainder when (pn − 1)n + (pn + 1)n is divided by pn2.
  124. Determining the kth element of the sorted radical function.
  125. Finding square sums that are palindromic.
  126. Exploring the number of cubes required to cover every visible face on a cuboid.
  127. Investigating the number of abc-hits below a given limit.
  128. Which tiles in the hexagonal arrangement have prime differences with neighbours?
  129. Investigating minimal repunits that divide by n.
  130. Finding composite values, n, for which n−1 is divisible by the length of the smallest repunits that divide it.
  131. Determining primes, p, for which n3 + n2p is a perfect cube.
  132. Determining the first forty prime factors of a very large repunit.
  133. Investigating which primes will never divide a repunit containing 10n digits.
  134. Finding the smallest positive integer related to any pair of consecutive primes.
  135. Determining the number of solutions of the equation x2 − y2 − z2 = n.
  136. Discover when the equation x2 − y2 − z2 = n has a unique solution.
  137. Determining the value of infinite polynomial series for which the coefficients are Fibonacci numbers.
  138. Investigating isosceles triangle for which the height and base length differ by one.
  139. Finding Pythagorean triangles which allow the square on the hypotenuse square to be tiled.
  140. Investigating the value of infinite polynomial series for which the coefficients are a linear second order recurrence relation.
  141. Investigating progressive numbers, n, which are also square.
  142. Perfect Square Collection
  143. Investigating the Torricelli point of a triangle
  144. Investigating multiple reflections of a laser beam.
  145. How many reversible numbers are there below one-billion?
  146. Investigating a Prime Pattern
  147. Rectangles in cross-hatched grids
  148. Exploring Pascal's triangle.
  149. Searching for a maximum-sum subsequence.
  150. Searching a triangular array for a sub-triangle having minimum-sum.
  151. Paper sheets of standard sizes: an expected-value problem.
  152. Writing 1/2 as a sum of inverse squares
  153. Investigating Gaussian Integers
  154. Exploring Pascal's pyramid.
  155. Counting Capacitor Circuits.
  156. Counting Digits
  157. Solving the diophantine equation 1/a+1/b= p/10n
  158. Exploring strings for which only one character comes lexicographically after its neighbour to the left.
  159. Digital root sums of factorisations.
  160. Factorial trailing digits
  161. Triominoes
  162. Hexadecimal numbers
  163. Cross-hatched triangles
  164. Numbers for which no three consecutive digits have a sum greater than a given value.
  165. Intersections
  166. Criss Cross
  167. Investigating Ulam sequences
  168. Number Rotations
  169. Exploring the number of different ways a number can be expressed as a sum of powers of 2.
  170. Find the largest 0 to 9 pandigital that can be formed by concatenating products.
  171. Finding numbers for which the sum of the squares of the digits is a square.
  172. Investigating numbers with few repeated digits.
  173. Using up to one million tiles how many different "hollow" square laminae can be formed?
  174. Counting the number of "hollow" square laminae that can form one, two, three, ... distinct arrangements.
  175. Fractions involving the number of different ways a number can be expressed as a sum of powers of 2.
  176. Rectangular triangles that share a cathetus.
  177. Integer angled Quadrilaterals.
  178. Step Numbers
  179. Consecutive positive divisors
  180. Rational zeros of a function of three variables.
  181. Investigating in how many ways objects of two different colours can be grouped.
  182. RSA encryption
  183. Maximum product of parts
  184. Triangles containing the origin.
  185. Number Mind
  186. Connectedness of a network.
  187. Semiprimes
  188. The hyperexponentiation of a number
  189. Tri-colouring a triangular grid
  190. Maximising a weighted product
  191. Prize Strings
  192. Best Approximations
  193. Squarefree Numbers
  194. Coloured Configurations
  195. Inscribed circles of triangles with one angle of 60 degrees
  196. Prime triplets
  197. Investigating the behaviour of a recursively defined sequence
  198. Ambiguous Numbers
  199. Iterative Circle Packing
  200. Find the 200th prime-proof sqube containing the contiguous sub-string "200"
  201. Subsets with a unique sum
  202. Laserbeam
  203. Squarefree Binomial Coefficients
  204. Generalised Hamming Numbers
  205. Dice Game
  206. Concealed Square
  207. Integer partition equations
  208. Robot Walks
  209. Circular Logic
  210. Obtuse Angled Triangles
  211. Divisor Square Sum
  212. Combined Volume of Cuboids
  213. Flea Circus
  214. Totient Chains
  215. Crack-free Walls
  216. Investigating the primality of numbers of the form 2n2-1
  217. Balanced Numbers
  218. Perfect right-angled triangles
  219. Skew-cost coding
  220. Heighway Dragon
  221. Alexandrian Integers
  222. Sphere Packing
  223. Almost right-angled triangles I
  224. Almost right-angled triangles II
  225. Tribonacci non-divisors
  226. A Scoop of Blancmange
  227. The Chase
  228. Minkowski Sums
  229. Four Representations using Squares
  230. Fibonacci Words
  231. The prime factorisation of binomial coefficients
  232. The Race
  233. Lattice points on a circle
  234. Semidivisible numbers
  235. An Arithmetic Geometric sequence
  236. Luxury Hampers
  237. Tours on a 4 x n playing board
  238. Infinite string tour
  239. Twenty-two Foolish Primes
  240. Top Dice
  241. Perfection Quotients
  242. Odd Triplets
  243. Resilience
  244. Sliders
  245. Coresilience
  246. Tangents to an ellipse
  247. Squares under a hyperbola
  248. Numbers for which Euler’s totient function equals 13!
  249. Prime Subset Sums
  250. 250250
  251. Cardano Triplets
  252. Convex Holes

blog comments powered by Disqus
Last Updated on Tuesday, 11 August 2009 16:34