diff --git a/C/p069.c b/C/p069.c index a55cc23..8f201e5 100644 --- a/C/p069.c +++ b/C/p069.c @@ -2,15 +2,15 @@ * relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6. * * n Relatively Prime φ(n) n/φ(n) - * 2 1 1 2 - * 3 1,2 2 1.5 - * 4 1,3 2 2 - * 5 1,2,3,4 4 1.25 - * 6 1,5 2 3 - * 7 1,2,3,4,5,6 6 1.1666... - * 8 1,3,5,7 4 2 - * 9 1,2,4,5,7,8 6 1.5 - * 10 1,3,7,9 4 2.5 + * 2 1 1 2 + * 3 1,2 2 1.5 + * 4 1,3 2 2 + * 5 1,2,3,4 4 1.25 + * 6 1,5 2 3 + * 7 1,2,3,4,5,6 6 1.1666... + * 8 1,3,5,7 4 2 + * 9 1,2,4,5,7,8 6 1.5 + * 10 1,3,7,9 4 2.5 * * It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. * @@ -26,41 +26,41 @@ int main(int argc, char **argv) { - int i, res = 1; + int i, res = 1; double elapsed; - struct timespec start, end; + struct timespec start, end; - clock_gettime(CLOCK_MONOTONIC, &start); + clock_gettime(CLOCK_MONOTONIC, &start); - i = 1; + i = 1; - /* Using Euler's formula, phi(n)=n*prod(1-1/p), where p are the distinct - * primes that divide n. So n/phi(n)=1/prod(1-1/p). To find the maximum - * value of this function, the denominator must be minimized. This happens - * when n has the most distinct small prime factor, i.e. to find the solution - * we need to multiply the smallest consecutive primes until the result is - * larger than 1000000.*/ - while(res < N) - { - i++; - - if(is_prime(i)) - { - res *= i; - } - } + /* Using Euler's formula, phi(n)=n*prod(1-1/p), where p are the distinct + * primes that divide n. So n/phi(n)=1/prod(1-1/p). To find the maximum + * value of this function, the denominator must be minimized. This happens + * when n has the most distinct small prime factor, i.e. to find the solution + * we need to multiply the smallest consecutive primes until the result is + * larger than 1000000.*/ + while(res < N) + { + i++; + + if(is_prime(i)) + { + res *= i; + } + } - /* We need the previous value, because we want i<1000000.*/ - res /= i; + /* We need the previous value, because we want i<1000000.*/ + res /= i; - clock_gettime(CLOCK_MONOTONIC, &end); + clock_gettime(CLOCK_MONOTONIC, &end); - elapsed = (end.tv_sec-start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; + elapsed = (end.tv_sec-start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; - printf("Project Euler, Problem 69\n"); + printf("Project Euler, Problem 69\n"); printf("Answer: %d\n", res); - printf("Elapsed time: %.9lf seconds\n", elapsed); + printf("Elapsed time: %.9lf seconds\n", elapsed); return 0; } diff --git a/Python/p022.py b/Python/p022.py index 9965a67..272c9f5 100644 --- a/Python/p022.py +++ b/Python/p022.py @@ -27,7 +27,7 @@ def p022(): sum_ = 0 i = 1 -# Calculate the score of each name an multiply by its position. + # Calculate the score of each name an multiply by its position. for name in names: l = len(name) score = 0 diff --git a/Python/p027.py b/Python/p027.py index cb2ae22..1f6e578 100644 --- a/Python/p027.py +++ b/Python/p027.py @@ -17,7 +17,8 @@ # 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. +# 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. from projecteuler import is_prime, timing diff --git a/Python/p045.py b/Python/p045.py index 867cef6..2f827be 100644 --- a/Python/p045.py +++ b/Python/p045.py @@ -2,8 +2,8 @@ # Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: # -# Triangle T_n=n(n+1)/2 1, 3, 6, 10, 15, ... -# Pentagonal P_n=n(3n−1)/2 1, 5, 12, 22, 35, ... +# Triangle T_n=n(n+1)/2 1, 3, 6, 10, 15, ... +# Pentagonal P_n=n(3n−1)/2 1, 5, 12, 22, 35, ... # Hexagonal H_n=n(2n−1) 1, 6, 15, 28, 45, ... # # It can be verified that T_285 = P_165 = H_143 = 40755. diff --git a/Python/p054.py b/Python/p054.py index 7477a5e..a1155ca 100644 --- a/Python/p054.py +++ b/Python/p054.py @@ -15,7 +15,7 @@ # 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 +# 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. # diff --git a/Python/p055.py b/Python/p055.py index 773af9a..c7f6953 100644 --- a/Python/p055.py +++ b/Python/p055.py @@ -11,8 +11,8 @@ # 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 +# 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). diff --git a/Python/p062.py b/Python/p062.py index b62d6f0..e35625d 100644 --- a/Python/p062.py +++ b/Python/p062.py @@ -1,6 +1,6 @@ #!/usr/bin/python -# The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). +# The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). # In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube. # # Find the smallest cube for which exactly five permutations of its digits are cube. diff --git a/Python/p069.py b/Python/p069.py index f540a09..95b72b4 100644 --- a/Python/p069.py +++ b/Python/p069.py @@ -3,15 +3,15 @@ # Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are # relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6. # -# n Relatively Prime φ(n) n/φ(n) -# 2 1 1 2 -# 3 1,2 2 1.5 -# 4 1,3 2 2 -# 5 1,2,3,4 4 1.25 -# 6 1,5 2 3 -# 7 1,2,3,4,5,6 6 1.1666... -# 8 1,3,5,7 4 2 -# 9 1,2,4,5,7,8 6 1.5 +# n Relatively Prime φ(n) n/φ(n) +# 2 1 1 2 +# 3 1,2 2 1.5 +# 4 1,3 2 2 +# 5 1,2,3,4 4 1.25 +# 6 1,5 2 3 +# 7 1,2,3,4,5,6 6 1.1666... +# 8 1,3,5,7 4 2 +# 9 1,2,4,5,7,8 6 1.5 # 10 1,3,7,9 4 2.5 # # It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. diff --git a/Python/p081.py b/Python/p081.py index da0289c..2cfab83 100644 --- a/Python/p081.py +++ b/Python/p081.py @@ -2,7 +2,7 @@ # In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, # is indicated in bold red and is equal to 2427. -# +# # *131* 673 234 103 18 # *201* *96* *342* 965 150 # 630 803 *746* *422* 111 diff --git a/Python/p082.py b/Python/p082.py index 560641a..cb3fe0e 100644 --- a/Python/p082.py +++ b/Python/p082.py @@ -11,7 +11,7 @@ # *201* *96* *342* 965 150 # 630 803 746 422 111 # 537 699 497 121 956 -# 805 732 524 37 331 +# 805 732 524 37 331 # # Find the minimal path sum, in matrix.txt, a 31K text file containing a 80 by 80 matrix, from the left column to the right column. diff --git a/Python/p083.py b/Python/p083.py index af4cbda..a689b5a 100644 --- a/Python/p083.py +++ b/Python/p083.py @@ -4,7 +4,7 @@ # # In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, # is indicated in bold red and is equal to 2297. -# +# # *131* 673 *234* *103* *18* # *201* *96* *342* 965 *150* # 630 803 746 *422* *111* diff --git a/Python/p088.py b/Python/p088.py index b43159c..0510a26 100644 --- a/Python/p088.py +++ b/Python/p088.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 -# A natural number, N, hat can be written as the sum and product of a given set of at least two natural numbers, {a1,a2,...,ak} is called a product sum number: N = a1 + a2 + ... + ak = a1 x a2 x ... ak. +# A natural number, N, hat can be written as the sum and product of a given set of at least two natural numbers, {a1,a2,...,ak} is called a product sum number: +# N = a1 + a2 + ... + ak = a1 x a2 x ... ak. # # For example, 6 = 1 + 2 + 3 = 1 x 2 x 3 # -# For a given set of size, k, we shall call the smallest N with this property a minimal product-sum number. The minimal product-sum numbers for sets of size, k = 2,3,4,5, and 6 are as follows. +# For a given set of size, k, we shall call the smallest N with this property a minimal product-sum number. The minimal product-sum numbers for sets of size, +# k = 2,3,4,5, and 6 are as follows. # # k = 2: 4 = 2 x 2 = 2 + 2 # k = 3: 6 = 1 x 2 x 3 = 1 + 2 + 3 diff --git a/Python/p090.py b/Python/p090.py index d8b22ac..d4d1356 100644 --- a/Python/p090.py +++ b/Python/p090.py @@ -1,22 +1,26 @@ #!/usr/bin/env python3 -# Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. +# Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side +# in different positions we can form a variety of 2-digit numbers. # # For example, the square number 64 could be formed: # |6||4| # -# In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81. +# In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: +# 01, 04, 09, 16, 25, 36, 49, 64, and 81. # # For example, one way this can be achieved is by placing {0,5,6,7,8,9} on one cube and {1,2,3,4,8,9} on the other cube. # -# However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0,5,6,7,8,9} and {1,2,3,4,6,7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09. +# However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0,5,6,7,8,9} and {1,2,3,4,6,7} allows +# for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09. # # In determining a distinct arrangement we are interested in the digits on each cube, not the order. # # {1,2,3,4,5,6} is equivalent to {3,6,4,1,2,5} # {1,2,3,4,5,6} is distinct from {1,2,3,4,5,9} # -# But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1,2,3,4,5,6,9} for the purpose of forming 2-digit numbers. +# But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1,2,3,4,5,6,9} +# for the purpose of forming 2-digit numbers. # # How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed? diff --git a/Python/p091.py b/Python/p091.py index 39a3faa..a0e414c 100644 --- a/Python/p091.py +++ b/Python/p091.py @@ -2,7 +2,8 @@ # The points P(x1,y1) and Q(x2,y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ. # -# There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is, 0<=x1,y1,x2,y2<=2. +# There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; +# that is, 0<=x1,y1,x2,y2<=2. # # Given that 0<=x1,y1,x2,y2<=50, how many right triangles can be formed? diff --git a/Python/p099.py b/Python/p099.py index b55bf82..1833247 100644 --- a/Python/p099.py +++ b/Python/p099.py @@ -1,9 +1,9 @@ -# Comparing two numbers written in index form like 2^11 and 3^7 is not difficult, as any calculator would confirm that +# Comparing two numbers written in index form like 2^11 and 3^7 is not difficult, as any calculator would confirm that # 2^11 = 2048 < 3^7 = 2187. # # However, confirming that 632382^518061 > 519432^525806 would be much more difficult, as both numbers contain over three million digits. # -# Using base_exp.txt, a 22K text file containing one thousand lines with a base/exponent pair on each line, +# Using base_exp.txt, a 22K text file containing one thousand lines with a base/exponent pair on each line, # determine which line number has the greatest numerical value. # # NOTE: The first two lines in the file represent the numbers in the example given above.*/ diff --git a/Python/projecteuler.py b/Python/projecteuler.py index b704936..a86785c 100644 --- a/Python/projecteuler.py +++ b/Python/projecteuler.py @@ -294,7 +294,7 @@ def is_semiprime(n, primes): return False, -1, -1 # Check if n is semiprime and one of the factors is 3. - elif n % 3 == 0: + if n % 3 == 0: if primes[n//3] == 1: p = 3 q = n // 3