Style improvement

This commit is contained in:
daniele 2024-02-20 19:46:29 +01:00
parent adca45e24f
commit 48503b8e27
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
16 changed files with 72 additions and 64 deletions

View File

@ -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. * 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) * n Relatively Prime φ(n) n/φ(n)
* 2 1 1 2 * 2 1 1 2
* 3 1,2 2 1.5 * 3 1,2 2 1.5
* 4 1,3 2 2 * 4 1,3 2 2
* 5 1,2,3,4 4 1.25 * 5 1,2,3,4 4 1.25
* 6 1,5 2 3 * 6 1,5 2 3
* 7 1,2,3,4,5,6 6 1.1666... * 7 1,2,3,4,5,6 6 1.1666...
* 8 1,3,5,7 4 2 * 8 1,3,5,7 4 2
* 9 1,2,4,5,7,8 6 1.5 * 9 1,2,4,5,7,8 6 1.5
* 10 1,3,7,9 4 2.5 * 10 1,3,7,9 4 2.5
* *
* It can be seen that n=6 produces a maximum n/φ(n) for n 10. * 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 main(int argc, char **argv)
{ {
int i, res = 1; int i, res = 1;
double elapsed; 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 /* 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 * 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 * 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 * 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 * we need to multiply the smallest consecutive primes until the result is
* larger than 1000000.*/ * larger than 1000000.*/
while(res < N) while(res < N)
{ {
i++; i++;
if(is_prime(i)) if(is_prime(i))
{ {
res *= i; res *= i;
} }
} }
/* We need the previous value, because we want i<1000000.*/ /* We need the previous value, because we want i<1000000.*/
res /= i; 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("Answer: %d\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed); printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0; return 0;
} }

View File

@ -27,7 +27,7 @@ def p022():
sum_ = 0 sum_ = 0
i = 1 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: for name in names:
l = len(name) l = len(name)
score = 0 score = 0

View File

@ -17,7 +17,8 @@
# where |n| is the modulus/absolute value of n # where |n| is the modulus/absolute value of n
# e.g. |11|=11 and |4|=4 # 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 from projecteuler import is_prime, timing

View File

@ -2,8 +2,8 @@
# Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: # Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
# #
# Triangle T_n=n(n+1)/2 1, 3, 6, 10, 15, ... # Triangle T_n=n(n+1)/2 1, 3, 6, 10, 15, ...
# Pentagonal P_n=n(3n1)/2 1, 5, 12, 22, 35, ... # Pentagonal P_n=n(3n1)/2 1, 5, 12, 22, 35, ...
# Hexagonal H_n=n(2n1) 1, 6, 15, 28, 45, ... # Hexagonal H_n=n(2n1) 1, 6, 15, 28, 45, ...
# #
# It can be verified that T_285 = P_165 = H_143 = 40755. # It can be verified that T_285 = P_165 = H_143 = 40755.

View File

@ -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 # 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. # 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) # n Relatively Prime φ(n) n/φ(n)
# 2 1 1 2 # 2 1 1 2
# 3 1,2 2 1.5 # 3 1,2 2 1.5
# 4 1,3 2 2 # 4 1,3 2 2
# 5 1,2,3,4 4 1.25 # 5 1,2,3,4 4 1.25
# 6 1,5 2 3 # 6 1,5 2 3
# 7 1,2,3,4,5,6 6 1.1666... # 7 1,2,3,4,5,6 6 1.1666...
# 8 1,3,5,7 4 2 # 8 1,3,5,7 4 2
# 9 1,2,4,5,7,8 6 1.5 # 9 1,2,4,5,7,8 6 1.5
# 10 1,3,7,9 4 2.5 # 10 1,3,7,9 4 2.5
# #
# It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. # It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10.

View File

@ -1,10 +1,12 @@
#!/usr/bin/env python3 #!/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 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 = 2: 4 = 2 x 2 = 2 + 2
# k = 3: 6 = 1 x 2 x 3 = 1 + 2 + 3 # k = 3: 6 = 1 x 2 x 3 = 1 + 2 + 3

View File

@ -1,22 +1,26 @@
#!/usr/bin/env python3 #!/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: # For example, the square number 64 could be formed:
# |6||4| # |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. # 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. # 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 equivalent to {3,6,4,1,2,5}
# {1,2,3,4,5,6} is distinct from {1,2,3,4,5,9} # {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? # How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?

View File

@ -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. # 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? # Given that 0<=x1,y1,x2,y2<=50, how many right triangles can be formed?

View File

@ -294,7 +294,7 @@ def is_semiprime(n, primes):
return False, -1, -1 return False, -1, -1
# Check if n is semiprime and one of the factors is 3. # 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: if primes[n//3] == 1:
p = 3 p = 3
q = n // 3 q = n // 3