Giter Site home page Giter Site logo

itzmeanjan / project-euler Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 869 KB

Another implementation of Project Euler Problems :wink: #ProjectEuler100

Home Page: https://itzmeanjan.github.io/project-euler/

License: MIT License

Go 100.00%
projecteuler100 projecteuler golang projecteuler-go

project-euler's Introduction

project-euler

Another implementation of Project Euler Problems 😉 #ProjectEuler100

motivation

I just came across one freecodecamp post, which tempted me to accept #ProjectEuler100 challenge. It's not like that prior to this I never thought of solving these beautiful mathematical problems, but I never opensourced it. So it looks like a great opportunity to me, where I can challenge my thinking and problem solving capability again.

solutions

I'm planning to stick to GoLang as language of implementation. All solutions will stay in this directory.

statement

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.

solution

233168 in 7.325µs

explanation

Iterating using a simple for loop ( starting from 3 ), upto (X - 1), where X is given, and checking divisibility of current number by either 3 or 5. If it's divisible, then we add it up to sum variable. And finally return sum, holding expected output.

statement

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.

solution

4613732 in 2.752µs

explanation

Using dynamic programming style, for calculating fibonacci terms, recursive strategy will be straightforward but run slow ( and no doubt very expensive ). Starting with a slice of two elements {1, 2}, we'll keep calculating next fibonacci term until most recently computed term crosses 4,000,000. And in each iteration, it'll check whether this term is even or not. If even, we'll add it up to sum variable, which is initialized with 2 ( because at very beginning fibArr, was only holding 2 as even number )

statement

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

solution

6857 in 171.188338ms

explanation

First calculates square root of given number, and find out all primes which are under or equals to that sqrt value. Now we'll simply iterate over that prime holder slice, from last to first, i.e. from higher value prime to lower value prime, cause finally, we need to find out maximum prime factor of num. That'll allow us to perform lesser number of checkings.

Generation of primes under X, is done using dynamic programming strategy, by updating a slice holding primes, on runtime. Because we know any composite number must have prime factor, lesser than square root of that number. So we'll perform check with prime numbers only, which will save a lot of computation too.

statement

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.

solution

906609 in 172.4µs

explanation

We'll start from end i.e. for finding largest possible palindrome number under 1000, we'll start checking from 999 & keep multiplying two numbers ( < 1000 ), until I reach 1. But that'll be brute-force, which is why we'd prefer breaking out of current iteration, as soon as current product ( product in this iteration ) goes below largestPalim ( which is largest palindrome computed upto this point ).

statement

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?

solution

232792560 in 1.664408611s

explanation

We'll start finding smallest number divisible by all numbers from 1 to 20, at 10, because for any number to be divisible by 10, it must end with round figure. And keep incrementing number under lens by 10 ( after each iteration ), which will eventually reduce #-of computational steps required for finding result.

statement

The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,(1 + 2 + ... + 10)2 = 552 = 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.

solution

25164150 in 275ns

explanation

We'll calculate square of sum of {1..100} ( used n*(n+1)/2, for finding sum of first n natural numbers ) & square of each natural number {1..100}, while accumulating them up in a single variable. Finally a simple absolute substraction of those two, will get us desired result.

statement

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?

solution

104743 in 14.523821ms

explanation

We'll buffer all primes calculated uptil now, and check a certain odd number's ( for reducing number of steps, we're skipping even numbers, cause they are definitely composite ) divisibility using primes ( buffered ) under square root of num. Finally we return (x-1) indexed term from buffer.

statement

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

solution

23514624000 in 36.693µs

explanation

Given a 1000 digit number ( as a string ), we'll iterate over all indices of this string ( from 0 to 999 ), so that we can consider all possible consequtive substrings of fixed length. Then we keep multiplying all digits present in each sub-sequence and take max of it. That'll satisfy our need, but multiplication of digits in subsequences is overlapping problem. So, we won't recompute, but use previous iterations cached value ( with care ), if and only if it's not initial iteration & first digit of previous subsequence isn't 0.

statement

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

solution

31875000 in 145.78477ms

explanation

We start iterating from c=999 ( outer loop ), b=998 ( mid loop ), a=997 ( inner most loop ), satisfying a < b < c always. Now we need to check whether a+b+c == 1000 or not, if it's lesser than so, we need to break out of loop, avoiding unnecessary computation. If previous condition and Pythagorean Triplet condition is met, we calculate product of a, b, c, which is our desired result.

statement

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

solution

142913828922 in 585.534206ms

explanation

First we'll start generating all primes under given number X, and then simply summing them up, will get us our desired result. For generating primes, we're going to make use of one method ( GeneratePrimesUnderX ) written during solving Problem 3.

statement

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

[ 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08

49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00

81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65

52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 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 03 45 02 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 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21

24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72

21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95

78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92

16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57

86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58

19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40

04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66

88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69

04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36

20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16

20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54

01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 ]

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?

solution

70600674 in 776.735µs

explanation

First we go for exploring neighboring element sequences, for any given index in GRID. Now any index can have 8 directions ( at max ) from it, i.e.

  • left
  • right
  • top
  • down
  • top-left
  • top-right
  • bottom-left
  • bottom-right

, given that a certain index which is calculated to be present in neighbor ( along any possible direction ), is valid, otherwise we don't explore elements along that direction. How many elements to be considered as neighbor, will be specified ( here it's 4 ). Now we'll simply calculate product of neighboring elements, and get max one for all indices. So, currently we're left with, 400 ( 20x20 GRID, so 400 cells ), products, all we've to do, is to pick up max number from that list, which is our desired result.

statement

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?

solution

perf_stat

explanation

This problem is pretty straightforward, all we've to do, is to keep generating triangular numbers, until we find one, which possesses factors >= 500. But if we plan to solve this problem deploying only one worker thread, it'll take a lot of time. So we're going to explore go-routines ( leveraging power of modern multi-core CPU architectures ), that's why this implementation is a bit lengthy. Multi-threaded ( green threads ) implementation details can be explored in source code.

statement

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

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

solution

5537376230 in 173.641µs

explanation

For handling arbitrary precision numbers, Golang provides us with "math/big" package, which is pretty handy in manipulating big numbers. After summing these 100 big numbers, we'll simply convert it to string, so that slicing first 10 digits become easier.

statement

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

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 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.

solution

837799 in 1.345537614s

explanation

For finding longest collatz sequence, generated by which number under 10^6, we're going to deploy 10^6 number of light-weight go-routines, for sake of faster computation. Next term of collatz sequence is generated by following rule

if n%2 == 0 {
	return n / 2
}
return 3*n + 1

We need to keep computing until we reach 1 ( because it's thought that all collatz sequences end at 1 ).

statement

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?

solution

137846528820 in 22m41.809635403s ( using Recursion )

137846528820 in 4.076µs ( using Bottom-Up approach i.e. Dynamic Programming )

explanation

Recursively computes possible number of paths for reaching bottom-right cell, while starting at top-left cell (0, 0). We can think of it like we're splitting into two child ( at max ) routes at every vertex, until we get to target vertex (2, 2) for example case.

lattice_path

Improvement - We can probably understand this problem is having following two properties

  • Optimal Substructure
  • Overlapping Subproblems

which are enough to indicate that this one can be solved using Dynamic Programming. So we're going to use a two dimentional array to store number of possible paths which can be taken from a certain index (i, j), to reach bottom-right most index (20, 20).

lattice_path_dp

statement

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?

solution

1366 in 20.087µs

explanation

Calculates sum of digits of base ^ pow, using math/big package of go standard library.

statement

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:

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

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)

solution

1074 in 16.624µs

explanation

I'll use following triangle for explanatory purpose.

3

7 4

2 4 6

8 5 9 3

We need to choose among 2^(n-1) = 2^3 = 8 paths from root to bottom level ( thinking of it as it's a binary tree ). Bruteforce won't help for larger size triangle, so we'll start updating node value of level 1 by accumulating node value of previous level ( root level is kept unchanged ), and continue this process. When we have multiple competiting values for a certain position, we'll choose max of it, cause our objective is to find max path value, running from top to bottom.

max_path_sum_1

max_path_sum_2

Thus 23 is max cost path, from top to bottom level. Algorithm's time and space complexity ∈ 0(n^2), where n = # of rows in triangle.

statement

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)?

solution

171 in 57.092µs

explanation

Starts iteration from 31/ 11/ 1899, which was Sunday ( given ), keeps calculating next Sunday, until we reach 01/ 01/ 2001. In each iteration we need to check, whether current date is within 01/01/1901 - 31/12/2000 && it's first day of month or not.

statement

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!

solution

648 in 25.651µs

explanation

First calculates factorial of any given number n as big.Int, after that we simply add digits up of that number, to get desired result.

statement

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

solution

31626 in 629.03783ms

explanation

We start from 1 and go upto 9999, for checking whether that number is forming amicable pair or not. Using caching mechanism, will help us in halving number of checks to be performed.

statement

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

solution

871198282 in 216.833152ms

explanation

We start by reading content of given data file, and converting it into a string slice ( slice holding names ). After that we'll go for a ascending sort of names. Now we need to calculate namescore of each name, following given rule. Finally we'll return sum of those scores.

statement

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

solution

4179871 in 17.033928895s

explanation

We'll start by creating a cache of abundant numbers < 28124, which will be used for checking whether we can represent any +ve integer < 28124, as a sum of two abudant numbers or not. Finally we'll return sum of those numbers which can't be written as sum of two abundant numbers.

statement

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

solution

2783915460 in 50.254669ms

explanation

Generating next lexicographic permutation using Indian mathematician Narayana Pandita's algorithm. Assuming we're given with a permutation of n-elements in a[n] array, now next lexicographic permutation can be found using following algorithm.

  • Find the largest index i such that a[i] < a[i + 1]. If no such index exists, the permutation is the last permutation.
  • Find the largest index j greater than i such that a[i] < a[j].
  • Swap the value of a[i] with that of a[j].
  • Reverse the sequence from a[i + 1] up to and including the final element a[n-1]

We'll keep generating next lexicographic permutation until we get to reach 10^6-th permutation.

statement

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1

F2 = 1

F3 = 2

F4 = 3

F5 = 5

F6 = 8

F7 = 13

F8 = 21

F9 = 34

F10 = 55

F11 = 89

F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the index of the first term in the Fibonacci sequence to contain 1000 digits?

solution

4782 in 63.3032ms

explanation

We'll keep generating fibonacci numbers, until we get first fibonacci term to have 1000 digits, with help "math/big" package. Index of that fibonacci term to be returned.

statement

Euler discovered the remarkable quadratic formula:

n^2 + n + 41

It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40,402+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41,41^2+41+41 is clearly divisible by 41.

The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79

The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form: n^2+an+b, where |a|<1000 and |b|≤1000 where |n| is the modulus/absolute value of n e.g. |11|=11 and |−4|=4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.

solution

-59231 in 4.852316977s

explanation

For leveraging power of multiple CPU cores of modern computers, we'll be using go-routines, for computing how many consequtive primes can be generated by ( n^2 + a*n + b ), for some given a & b combination. And we'll cache maximum value computed upto this point, which will be returned at end of computation.

statement

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25

20 7 8 9 10

19 6 1 2 11

18 5 4 3 12

17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

solution

669171001 in 26.457464ms

explanation

We'll start by calculating field values of sub-matrix of size 3 x 3, centered at intersection of two diagonals of matrix. Center field of matrix is holding 1. We'll keep calculating these field values, upto size <= 1001, increasing size by 2 at each iteration.

spiral_diagonal

And finally, sum of all values present on two diagonals of matrix, to be returned.

statement

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2^2=4, 2^3=8, 2^4=16, 2^5=32

3^2=9, 3^3=27, 3^4=81, 3^5=243

4^2=16, 4^3=64, 4^4=256, 4^5=1024

5^2=25, 5^3=125, 5^4=625, 5^5=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?

solution

9183 in 32.185177ms

explanation

We'll simply use one hash map to keep track of number unique terms generated by a^b, 2 <= a <= 100 & 2 <= b <=100, and cheers to "math/big" package.

statement

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1^4 + 6^4 + 3^4 + 4^4

8208 = 8^4 + 2^4 + 0^4 + 8^4

9474 = 9^4 + 4^4 + 7^4 + 4^4

As 1 = 1^4 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.

solution

443839 in 406.554596ms

explanation

We'll start checking whether a certain number equals to sum of fifth power of their digits, from 10 and go upto 10^6, while keeping track of sum of numbers which are satisfying this criteria.

statement

In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).

It is possible to make £2 in the following way:

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p

How many different ways can £2 be made using any number of coins?

solution

73682 in 3.532µs

explanation

We can make 3p using { 1p } coins, only in one possible way i.e. { 1p + 1p + 1p }. But when given with { 1p, 2p } coins, we'll have one more possible way { 1p + 2p } of making 3p. Now our target it to count £2 ( = 200p ) using 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p) coins, where it's clearly visible that multiple subproblems are overlapping, and it also has an optimal substructure property. So we'll prefer dynamic programming to recursion, for counting possible number of ways. We'll use one single dimentational array of length (targetV + 1) = 201, where we'll store possible number of ways for making all values from 0p to 200p, using given coins.

statement

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

solution

45228 in 12.040034ms

explanation

We'll iterate over a range (2-9999), and explore all possible products of them, so that we don't miss any possible multiplicand/ multiplier/ product combination, for which pandigital criteria gets satisfied. Now we need to also consider one matter that some products can be obtained in multiple ways, so we'll simply choose to use hash map for storing products, so that we don't count any certain product more than ones. For reducing number of checks being performed, we'll only check those combinations which will have 9 digits total i.e. ( digitCount(multiplicand) + digitCount(multiplier) + digitCount(product) ) == 9, cause we're checking pandigital products from 1 through 9.

statement

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

solution

100 in 8.947783ms

explanation

First we'll generate all those fractions, having exactly two digits in both numerator & denominator, and not having 0 as digit. In next step, we'll go for extracting curious fractions ( as per given definition ). After that, we'll multiply those filtered out fractions, which will be represented in its lowest common terms. Finally we'll return denominator of fraction.

statement

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.

solution

40730 in 13.280042ms

explanation

First we'll build a slice holding all digits ( 0 - 9 ) of a given number, which will then be updated with each digits corresponding factorial values, ( which will stay precomputed in a buffer i.e. hash map ). Then we'll sum up that slice and compare it with given number, if they are equal, it's curious number, which are searching for. We'll find all curious numbers, and sum them up. By the way, we've only two curious numbers 145 & 40225

statement

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

solution

55 in 1.256801259s

explanation

We'll keep one hash map holding all primes explored yet, because while checking 13, which is a prime, we also check its rotated form i.e. 31, which is also prime, so 13 and 31 both are circular primes. But in some future iteration, if we go for checking 31 again, that'll result into recomputation of precomputed values, wasting computational resources. Finally length of prime holder hash map to be returned.

statement

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

solution

872187 in 26.343636ms

explanation

We check all numbers from 2 to 10^6, whether they are palindrome or not, of yes, then we convert it into binary, which is then checked for palindrome nature; else we go for next decimal number. Finally we return sum of all numbers, which are palindrome in both bases ( 2 & 10 ).

statement

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

solution

748317 in 583.690604ms

explanation

We'll start checking from 10, and keep moving forward until we get to explore 11 fully truncatable primes. For each given number, we'll first check whether number is left truncatable. If yes, we go for right truncatability check, else we say it's not fully truncatable. Finally sum of those 11 numbers to be returned.

statement

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192

192 × 2 = 384

192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?

solution

932718654 in 10.566131ms

explanation

We'll start from 2 and go upto 9876, for each number we'll keep it multiplying with numbers {1, 2, .., n}, where n > 1 ( and to be incremented by 1 in each iteration ), and their concatenated products to be checked for pandigital nature from 1 though 9. To reduce number of checks, we'll only consider those numbers having exactly 9 digits. For numbers having lesser digits, we'll skip to next iteration. And for numbers with digit count > 9, we'll break out of this checking loop, and consider next multiplicand. Multiplicands to be considered are from 2 to 9876, due to the fact that, for any number greater than that, we'll always loose 9 digit lengthy concatenated product criteria. E.g. lets take 9877, and products generated by multiplying {1, 2}, and after concatenation, it'll be {9, 8, 7, 7, 1, 9, 7, 5, 4}, which is 9 digit lengthy, but not pandigital, but when we try to multiply it with {1, 2, 3}, their concatenated products will make {9, 8, 7, 7, 1, 9, 7, 5, 4, 2, 9, 6, 3, 1}, which is definitely not 9 digit, so we don't need to check them at all.

pandigital_multiples

Finally we'll return maximum 1 though 9 pandigital number, that can be obtained this way.

statement

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

solution

interger_right_triangle

explanation

We need to ensure for some combination of a, b, c, a + b + c == p && a^2 + b^2 == c^2 must be satisfied for that a, b, c combination to be considered as valid. We'll try to find out how many number of ways we can form a right triangle, when p is given, by generating a, b, c. Our job is to find out maximum p < 1001, so that number of right triangles generated is maximum.

statement

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

solution

210 in 11.470216ms

explanation

We'll not store all 10^6 digits in a slice, so that later we can compute required equation value by looking up values from slice positions, rather we'll choose one tricky way, in which only 1-5 digits ( at max ) will be kept at a time & stopDigit positions i.e. 1, 10, 100, 1000, 10000, 100000, 1000000 will be kept track of, when close by we'll pick digit at corresponding stopDigit position in a hash map, which will be later used for generating result of given equation.

statement

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

solution

7652413 in 2m33.647939438s

explanation

We'll keep calculating maximum number that can be represented using n-digits, which is also pandigital & prime, in several go-routines. Each go-routine will report to listener. In hope of performing lesser number of computations, we'll start checking from 9999 ( for 4 digit pandigital prime number ), and stop as soon as we find a single number satisfying both conditions, which is 4231. Finally we'll return maximum n-digit pandigital prime number, it's pretty much understandable that maximum value of n can be 9.

statement

The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

solution

162 in 2.508198ms

explanation

We'll read given file content and obtain a slice of words, and each of them will be sent to one function for checking their triangle property. For each word, we'll compute its word value, and check whether that number is triangular or not. Finally number of triangle words to be returned.

statement

The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.

Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:

d2d3d4=406 is divisible by 2

d3d4d5=063 is divisible by 3

d4d5d6=635 is divisible by 5

d5d6d7=357 is divisible by 7

d6d7d8=572 is divisible by 11

d7d8d9=728 is divisible by 13

d8d9d10=289 is divisible by 17

Find the sum of all 0 to 9 pandigital numbers with this property.

solution

16695334890 in 1.260754677s

explanation

We need to keep checking all 0-9 pandigital numbers, which are satisfying criteria given in above question. We can do either of two things :

  • Start with first 10 digit number, and keep checking all 10 digit numbers, whether they are 0-9 pandigital and satisfying above given criteria
  • Or start with smallest 10 digit number that is 0-9 pandigital, and keep generating lexicographic permutations until we get 0 as first digit of permutation i.e. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], and in each pass, we'll check whether that number satifies above given criterias or not.

Obviously second choice is better, as we're going to check lesser number and it's always guaranted that we're checking a number which is 0-9 pandigital i.e. we can simply skip pandigital check.

Lets take an example, 102, which is 0-2 pandigital, can only generate 0-2 pandigital permutations, when we generate next term using lexicographic permutation generation rule i.e. 120, 201, 210. And next term generated is 012, which is very first term and having 0 at beginning, so it's not really a 0-2 pandigital number, rather it's a 1-2 pandigital number. Which is why we're checking until we get first digit as 0 in case of 0-9 pandigital numbers.

Finally we'll add those 0-9 pandigital numbers, which are satisfying above given criteria.

statement

Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:

1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...

It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.

Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?

solution

5482660 in 7.281894631s

explanation

We'll keep track of all pentagonal numbers generated, and once a new pentagonal number is generated we'll combine it with all remaining elements; and that pair to be checked for following condition

  • Is a + b Pentagonal ?
  • Is |a - b| Pentagonal ?

If both of them are yes, only them, we can stop searching for desired pair, and return absolute difference of those two pentagonal numbers, which is also a pentagonal number.

statement

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...

Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ...

Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

solution

1533776805 in 1.659591111s

explanation

We'll keep computing next triangular number ( starting from 286-th triangular number ) , until we find one which is pentagonal and hexagonal too.

statement

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

9 = 7 + 2×1^2

15 = 7 + 2×2^2

21 = 3 + 2×3^2

25 = 7 + 2×3^2

27 = 19 + 2×2^2

33 = 31 + 2×1^2

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

solution

5777 in 7.999267ms

explanation

We'll keep a buffer of primes, which will be first filled up, by generating all primes under 100. Now we'll start checking from 35 and keep checking all odd numbers, until we find one which doesn't satisfy Goldbach's Conjecture. To avoid huge recomputation of primes, we'll keep modifying that same slice of primes whenever we run out of sufficient number of primes.

statement

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7 15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23

645 = 3 × 5 × 43

646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?

solution

134043 in 2.836098132s

explanation

We'll start with 1, 2, 3, 4, for each of them we'll find their unique prime factors and keep it in cache, if for each of them unique prime factor count satisfies given count value, then we've obtained our solution. Until we find such a solution we'll keep exploring next combinations. Now for avoiding huge recomputation of primes, we'll no doubt cache them and use it in successive computations. Another thing to be noticed, if we explore 1, 2, 3, 4, in next iteration we'll explore 2, 3, 4, 5 and next to that 3, 4, 5, 6 i.e. in each iteration we need to only compute a single element's prime factors, others are precompted, which saves a lot of CPU cycles.

statement

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.

solution

9110846700 in 6.437555ms

explanation

Using help of "math/big" of golang, we'll keep computing power of x^x & summing them up in an accumulator, as long as x <= 1000. Finally returned last 10 digits of sum of series.

statement

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways:

(i) each of the three terms are prime, and,

(ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

solution

296962999629 in 2m44.957077262s

explanation

First we'll generate all primes having four digits, now we'll keep choosing three primes ( from that set ) in all possible ways. And each of them to validated against criterias given in question, if it satisfies so & other than { 1487, 4817, 8147 }, then we've obtained our desired solution. Because it's already given that we've only two sequence such that.

statement

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

solution

997651 in 7.538766769s

explanation

First we'll generate all primes under 10^6, and keep them in a given slice. Now we'll compute sum of those primes. Our objective is to find out longest consecutive prime sequence, which has sum < 10^6. So we need two pointers, one pointing at beginning of sequence ( i.e. starting index of sequence under consideration ) & another pointing at end of sequence, which is to be modified by inner loop index. And front index gets controlled by outer loop. For avoiding huge recomputation of primes, we'll cache previous iteration's sum, and substract last term of previous sequence, to get sum of current sequence, which is simply one lesser than previous iterations last index.

consecutive_prime_sum

statement

By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

solution

121313 in 4.654042749s

explanation

We'll keep generating primes of certain length, starting from 2. For each of them, we'll try to replace 1 to len(prime)-1, number of digits by 0-9, which will eventually generate <= 10 new numbers ( at min 9 ). Now we'll check whether this series is having x number of primes or not. If yes, we'll return starting number of this prime series, else we'll go for exploring another prime.

statement

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

solution

142857 in 206.462802ms

explanation

We'll keep exploring ( starting with 1 ) until we get one such number, which is when multplied with each of {2, 3, 4, 5, 6}, will be permutation of same digits

statement

There are exactly ten ways of selecting three from five, 12345:

123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

In combinatorics, we use the notation, (5C3)=10

In general, (nr)=n!r!(n−r)!, where r≤n, n!=n×(n−1)×...×3×2×1, and 0!=1

It is not until n=23, that a value exceeds one-million: (23C10)=1144066

How many, not necessarily distinct, values of (nCr) for 1≤n≤100, are greater than one-million?

solution

4075 in 60.199108ms

explanation

We're going to leverage caching mechanism heavily, for choosing r many elements from n many elements, where 1 <= r <= n, by building pascal triangle only once for a certain n. And we'll keep track of how many of those choice counts are >10^6, which will be returned.

statement

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

High Card: Highest value card.

One Pair: Two cards of the same value.

Two Pairs: Two different pairs.

Three of a Kind: Three cards of the same value.

Straight: All cards are consecutive values.

Flush: All cards of the same suit.

Full House: Three of a kind and a pair.

Four of a Kind: Four cards of the same value.

Straight Flush: All cards are consecutive values of same suit.

Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:

2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD 2C 3S 8S 8D TD Player 2
2 5D 8C 9S JS AC 2C 5C 7D 8S QH Player 1
3 2D 9C AS AH AC 3D 6D 7D TD QD Player 2
4 4D 6S 9H QH QC 3D 6D 7H QD QS Player 1
5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

solution

376 in 180.557576ms

explanation

Coming soon

statement

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.

solution

249 in 143.031404ms

explanation

While leveraging power of multicore CPU using go-routines, we'll parallelly compute how many numbers are lychrel under 10K. Thanks to math/big for handling big numbers efficiently.

statement

A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?

solution

972 in 26.468845ms

explanation

Iteratively computes maximum sum of digits of natural number of form a^b where a, b < 100.

statement

It is possible to show that the square root of two can be expressed as an infinite continued fraction.

√2 = 1+1/(2+1/(2+1/2+…

By expanding this for the first four iterations, we get:

1+1/2=3/2=1.5

1+1/(2+1/2)=7/5=1.4

1+1/(2+1/(2+1/2))=17/12=1.41666…

1+1/(2+1/(2+1/(2+1/2)))=41/29=1.41379…

The next three expansions are 9970, 239169, and 577408, but the eighth expansion, 1393985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator

solution

153 in 10.379267ms

explanation

We can find a clear pattern in calculating next expanded form of √2, given intial form

p / q = (num(i-1)+ den(i-1)*2) / (num(i-1) + den(i-1))

Now it's very easy to count how many of these fractions having more digits in numerator than in denominator.

statement

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

37 36 35 34 33 32 31

38 17 16 15 14 13 30

39 18 5 4 3 12 29

40 19 6 1 2 11 28

41 20 7 8 9 10 27

42 21 22 23 24 25 26

43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

solution

26241 in 1.652747178s

explanation

We'll generate only diagonal elements of square matrix of certain order ( i.e. 1x1, 3x3, 5x5 ... ) in clockwise spiral fashion. And we'll keep doing that thing until we get a prime ratio < 10%. Now in each iteration we need to check how many of these diagonal elements are prime, to avoid checking all diagonal elements in each iteration, we'll cache prime count of previous iteration and only check newly generated four corner items. For reduing space complexity, we'll store only 4 corner elements of square matrix, in each iteration.

More coming soon ... 😉

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.