The post Project Euler 10 Solution appeared first on Dreamshire.
]]>The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Sieve the prime numbers less than 2,000,000 and sum them. Having a fast algorithm to sieve primes helps a solution run under a second.
prime_sieve
is listed in Common Functions and Routines for Project EulerThe post Project Euler 10 Solution appeared first on Dreamshire.
]]>The post Project Euler 11 Solution appeared first on Dreamshire.
]]>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
…Data Continues…
The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?
No other option but to read the matrix into an array and iterate through the rows, columns and diagonals to find four adjacent cells that produce a maximum product. Only right, down, diagonal down-left and diagonal down-right need to be checked because of the commutative nature of multiplication.
This program assumes the same number of rows and columns.
The post Project Euler 11 Solution appeared first on Dreamshire.
]]>The post Project Euler 12 Solution appeared first on Dreamshire.
]]>The sequence of triangle numbers is generated by adding the natural numbers. So the 7^{th} 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:
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?
REVISED
Our algorithm for solving this one was improved nicely by a detailed explanation provided by a Project Euler moderator named ‘rayfil’. His wonderful explanation is published as a PDF document that is available after you solve the problem. What follows is our own interpretation written in Python.
If you run the Trinket for an input of 500 it will take about 15 seconds to run.
The post Project Euler 12 Solution appeared first on Dreamshire.
]]>The post Project Euler 13 Solution appeared first on Dreamshire.
]]>Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
Corrected
The numbers to add were saved to a file, pe13.txt, read in, and summed. The sum was truncated keeping the first 10 digits.
Have a great day – and check out some of my other pages.
Thanks,
your friend,
Mike
June 8, 2014
The post Project Euler 13 Solution appeared first on Dreamshire.
]]>The post Project Euler 14 Solution appeared first on Dreamshire.
]]>Project Euler 14: The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
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.
The Python program listed in the Afterthoughts section shows the hailstone
calculator for determining values in the Collatz chain for a number, N. The d
function determines the length of the chain and the purpose of the program is to determine the longest length for a set of consecutive numbers below some limit, L
. It became apparent that the lengths were simply a collection of discrete points over the domain.
So, when presented by other Project Euler 14 problems hosted on other sites, with more aggressive limits (N<=10,000,000), and running thousands of trials in fewer than 5 seconds it became obvious that building a table was the best way to go.
There are fragments of this sequence in the OEIS but a problem was the requirement to find the maximum starting value when the same chain length is shared by other starting numbers.
For example 35497 is not found in these on-line sequences and for an N=34555 the starting number for the longest chain returned is 34239. Well it seems the 35497 shares the same number of terms and is the greater of the two. 35497 is the correct response.
This required going backwards through a range of numbers (say 10^{7}) and finding the stopping points and taking these maximums into consideration. That result is the list shown in the Python source code below.
c = [1, 2, 3, 6, 7, 9, 18, 19, 25, 27, 54, 55, 73, 97, 129, 171, 231, 235, 313, 327, 649, 654,
655, 667, 703, 871, 1161, 2223, 2322, 2323, 2463, 2919, 3711, 6171, 10971, 13255, 17647,
17673, 23529, 26623, 34239, 35497, 35655, 52527, 77031, 106239, 142587, 156159, 216367,
230631, 410011, 511935, 626331, 837799, 1117065, 1126015, 1501353, 1564063, 1723519,
2298025, 3064033, 3542887, 3732423, 5649499, 6649279, 8400511, 11200681]
L = 1000000
print min(c[::-1], key=lambda x: x>L)
Use this link to get the Project Euler 14 Solution Python 2.7 source.L = 1000000
hailstone = lambda n: 3*n + 1 if n%2 else n//2
def d(n, _={1:1}):
if n not in _: _[n] = d(hailstone(n)) + 1
return _[n]
print max(range(1, L), key=d)
The post Project Euler 14 Solution appeared first on Dreamshire.
]]>The post Project Euler 15 Solution appeared first on Dreamshire.
]]>Project Euler 15: Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20×20 grid?
This question has been posed as: “Starting at the top left corner, how many ways through town are possible using only one-way streets and ending at the bottom right corner of a n x n grid?”
Another analogy might be: “Place a single rook on the top left corner of an empty chess board and count the number of tours to the bottom right corner moving only left and down.” This would assume an 8 × 8 grid.
This solution extends to solving n × m grids as well as the n × n grid by using a simple combination calculation. For example, the valid paths for a 2 × 2 grid shown in the example are the discreet (unique) permutations of {R, R, D, D}. We can list these as: {R, R, D, D}, {R, D, R, D}, {R, D, D, R}, {D, R, R, D}, {D, R, D, R}, and {D, D, R, R}. You must have n Rs (rights) and n Ds (downs) the order is not important as long as you start from the top corner you will always end at the bottom corner.
For a 4 × 4 grid it would be the discreet permutations of {R, R, R, R, D, D, D, D} where any combination or 4 Rs and 4 Ds will always be a valid path. If the Rs are place randomly in 4 of the 8 slots first they can be considered independent and the Ds would be considered dependent because they have to be place into the remaining open slots.
For example, placing the 4 Rs in any of the available 8 slots as {R, _, R, R, _, R, _, _} will dictate where the Ds get placed as {R, D, R, R, D, R, D, D}. You end up with just _{8}C_{4} = 70 valid combinations.
It has also be described as how many distinct ways can you shuffle the characters in the string “RRRRDDDD”.
Now, determining the number of contiguous routes for a square grid (n × n) is the central binomial coefficient or the center number in the 2n^{th} row of Pascal's triangle.
The formula in general for any rectangular grid (n × m) using the notation for the binomial coefficient is:
The post Project Euler 15 Solution appeared first on Dreamshire.
]]>The post Project Euler 16 Solution appeared first on Dreamshire.
]]>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}?
Python supports arbitrary precision integers so calculating any obscenely large number can be done without external libraries.
Calculate 2^{1000}, split the digits and add them together. The str()
function is used as an iterator and serves each character in 2^{1000} until it reaches the end of the string.
I’m sure when this problem was created it was intended to be solved without large integer support or string manipulations, but such is the way of modern languages.
def sum_of_digits(n):
r = 0
n = int(abs(n))
while n:
r, n = r + n % 10, n / 10
return r
The post Project Euler 16 Solution appeared first on Dreamshire.
]]>The post Project Euler 17 Solution appeared first on Dreamshire.
]]>If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.
Converted the integers from 1 to 999 to the written equivalent, removed spaces and hyphens, added the character count for each one, including “onethousand”.
Although this is written in the long, lost language of Perl, it is quite extensible for converting a number to its English-word equivalent. So, instead of solving the problem directly, I wrote this to solve more general problems of similar nature.
Python would also handle this job nicely, but there is still a hopeless romantic attachment to Perl that, I think, I’ll never lose. Hope you enjoy the solution in a throwback language.
@D{0 .. 20, 30,40,50,60,70,80,90} = qw|
zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
twenty thirty forty fifty sixty seventy eighty ninety|;
for $i (1..999) {
$x= num2en($i); $x=~s/[\s\-]//g;
$l+= length($x);
}
print "Answer to PE17 = ",$l+length('onethousand');
sub num2en {
my($x) = $_[0];
return $D{$x} if defined $D{$x};
return $D{$1 . '0'} . '-' . $D{$2} if( $x =~ m/^(.)(.)$/ );
if ( $x =~ m/^(.)(..)$/ ) {
my($h, $rest) = ("$D{$1} hundred", $2);
return $h if $rest eq '00';
return "$h and " . num2en($rest);
}
}
Use this link to get the Project Euler 17 Solution Perl source.The post Project Euler 17 Solution appeared first on Dreamshire.
]]>The post Project Euler 18 Solution appeared first on Dreamshire.
]]>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 5
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
… {data continues} …
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)
To solve this problem and problem 67, which is much larger, start the search from the bottom to the top, adding the maximums along the way. This will “bubble” the maximum total to the top of the pyramid.
Let’s follow this technique, step by step, with the 4 row triangle example above to show how this works.
3
7 5
2 4 6
8 5 9 3
Starting at the bottom,
We look at 8 and 5, pick the maximum, 8 in this case, and replace the 2 in the previous row with their sum 10.
We look next at 5 and 9, pick the maximum, 9, and replace the 4 in the previous row with their sum 13.
We look lastly at 9 and 3, pick the maximum, 9, and replace the 6 in the previous row with their sum 15.
Now our array looks like:
3
7 5
10 13 15
Let’s do it again. Take the larger of 10 and 13 and add it to 7 making 20.
Take the larger of 13 and 15 and add it to 5 making 20.
Now our array looks like:
3
20 20
At last we take the larger of 20 and 20 (yes, I know they’re the same) and add it to 3 making 23.
And our array looks like:
23
The maximum total path in the triangle.
The post Project Euler 18 Solution appeared first on Dreamshire.
]]>The post Project Euler 19 Solution appeared first on Dreamshire.
]]>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)?
There are 1200 months (therefore 1200 firsts of the month) in 100 years (1/1/1901 – 12/31/2000). One in every 7 days is a Sunday (or Monday, Tuesday, etc.) which are (roughly) uniformly distributed over the date range. An estimate of 1200/7 Sundays should be close to the answer. As the date range widens this estimate becomes less precise.
The Python program below cycle through each day and counts Sundays on the first of the month. It would be nice to simply cycle through the months, but Python doesn’t make that easy.
The calendar completes a full cycle every 400 years, so if a year is specified with 16 digits, as is the case at the HackerRank.com site, simply modulus it by 400 and add 2400 (arbitrary choice) to get the year into a range that works with Python. So 2016 and 2416 will use the same calendar. If you don’t cross a div by 400 year boundary, then the calendar repeats every 28 years, so 2016 and 2044 will also share the same calendar.
Here’s a site for generating calendars for any year: Calendar home
Read more: Patterns in the Perpetual Calendar
import datetime as dt
delta = dt.timedelta(days=1)
dow = 6 #Sunday
day = 1 #date
start_date = s = dt.datetime(1901, 1, 1)
end_date = dt.datetime(2000, 12, 31)
c = 0
while start_date <= end_date:
if start_date.day==day and start_date.weekday()==dow: c+=1
start_date += delta
print "Days on the 1st of the month between", s.strftime("%d-%b-%Y"), \
"\nand", end_date.strftime("%d-%b-%Y"), "inclusive:", c
Use this link to get the Project Euler 19 Solution Python source.Also, some think that using date or calendar libraries is taking unfair advantage when solving these kind of problems. Nothing could be further from the truth. You need to leverage well vetted software in order to speed development time and reduce errors. Using established extensions and packages is simply good practice.
The post Project Euler 19 Solution appeared first on Dreamshire.
]]>