Correct problem 9 solution
This commit is contained in:
parent
3a09f3d4f5
commit
2cdbca922a
28
C/p009.c
28
C/p009.c
@ -11,10 +11,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include "projecteuler.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int a, b, c, m, n, found = 0;
|
int a, b, c, tmpa, tmpb, tmpc, i, m, n, found = 0;
|
||||||
double elapsed;
|
double elapsed;
|
||||||
struct timespec start, end;
|
struct timespec start, end;
|
||||||
|
|
||||||
@ -24,7 +25,9 @@ int main(int argc, char **argv)
|
|||||||
* Euclid's formula, until the one where a+b+c=1000 is found.*/
|
* Euclid's formula, until the one where a+b+c=1000 is found.*/
|
||||||
for(m = 2; found == 0; m++)
|
for(m = 2; found == 0; m++)
|
||||||
{
|
{
|
||||||
for(n = 1; n < m; n++)
|
for(n = 1; n < m && !found; n++)
|
||||||
|
{
|
||||||
|
if(gcd(m, n) == 1 && (m % 2 == 0 && n % 2 != 0 || m % 2 != 0 && n % 2 == 0))
|
||||||
{
|
{
|
||||||
a = m * m - n * n;
|
a = m * m - n * n;
|
||||||
b = 2 * m * n;
|
b = 2 * m * n;
|
||||||
@ -35,6 +38,27 @@ int main(int argc, char **argv)
|
|||||||
found = 1;
|
found = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i = 2;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
tmpa = a * i;
|
||||||
|
tmpb = b * i;
|
||||||
|
tmpc = c * i;
|
||||||
|
|
||||||
|
if(tmpa + tmpb + tmpc == 1000)
|
||||||
|
{
|
||||||
|
a = tmpa;
|
||||||
|
b = tmpb;
|
||||||
|
c = tmpc;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}while(tmpa + tmpb + tmpc < 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
#
|
#
|
||||||
# Find the product abc.
|
# Find the product abc.
|
||||||
|
|
||||||
|
from math import gcd
|
||||||
|
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -23,6 +25,10 @@ def main():
|
|||||||
# Euclid's formula, until the one where a+b+c=1000 is found.
|
# Euclid's formula, until the one where a+b+c=1000 is found.
|
||||||
while not found:
|
while not found:
|
||||||
for n in range(1, m):
|
for n in range(1, m):
|
||||||
|
if found == 1:
|
||||||
|
break
|
||||||
|
|
||||||
|
if gcd(m, n) == 1 and (m % 2 == 0 and n % 2 != 0 or m % 2 != 0 and n % 2 == 0):
|
||||||
a = m * m - n * n
|
a = m * m - n * n
|
||||||
b = 2 * m * n
|
b = 2 * m * n
|
||||||
c = m * m + n * n
|
c = m * m + n * n
|
||||||
@ -31,6 +37,25 @@ def main():
|
|||||||
found = 1
|
found = 1
|
||||||
break
|
break
|
||||||
|
|
||||||
|
i = 2
|
||||||
|
|
||||||
|
while True:
|
||||||
|
tmpa = a * i
|
||||||
|
tmpb = b * i
|
||||||
|
tmpc = c * i
|
||||||
|
|
||||||
|
if tmpa + tmpb + tmpc == 1000:
|
||||||
|
a = tmpa
|
||||||
|
b = tmpb
|
||||||
|
c = tmpc
|
||||||
|
found = 1
|
||||||
|
break
|
||||||
|
|
||||||
|
if tmpa + tmpb + tmpc > 1000:
|
||||||
|
break
|
||||||
|
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
m = m + 1
|
m = m + 1
|
||||||
|
|
||||||
end = default_timer()
|
end = default_timer()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user