Style improvement
This commit is contained in:
parent
adca45e24f
commit
48503b8e27
66
C/p069.c
66
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++;
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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?
|
||||
|
||||
|
@ -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?
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user