Complete the first 10 problems
This commit is contained in:
commit
5243ddad4b
|
@ -0,0 +1 @@
|
|||
a.out
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int a = 1; a < 1000; a++)
|
||||
{
|
||||
if (a % 3 == 0 || a % 5 == 0)
|
||||
sum += a;
|
||||
}
|
||||
printf("The sum of all multiples of 3 or 5 below 1000 is: %d\n", sum);
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int
|
||||
isPrime(int number)
|
||||
{
|
||||
if (number <= 1)
|
||||
return 0;
|
||||
for (int i = 2; i * i <= number; i++)
|
||||
if (number % i == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
long sum = 0;
|
||||
for(int i = 0; i < 2000000; i++)
|
||||
{
|
||||
if (isPrime(i))
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
printf("The sum of all the primes below 2,000,000 is %ld\n", sum);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int num = 1;
|
||||
int prevnum = 1;
|
||||
int sum = 0;
|
||||
while (num <= 4000000)
|
||||
{
|
||||
if (num % 2 == 0)
|
||||
sum += num;
|
||||
int store = num;
|
||||
num = num + prevnum;
|
||||
prevnum = store;
|
||||
}
|
||||
printf("The sum of the even-valued terms of the Fibonacci sequence whose values do not exceed four million is: %d\n", sum);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
static int
|
||||
isPrime(long number)
|
||||
{
|
||||
if (number <= 1)
|
||||
return 0;
|
||||
for (long i = 2; i * i <= number; i++)
|
||||
if (number % i == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const long number = 600851475143;
|
||||
long remainder = number;
|
||||
long largestPrimeFactor = 1;
|
||||
float percentComplete = 0.00;
|
||||
|
||||
while (remainder % 2 == 0)
|
||||
remainder /= 2;
|
||||
while (remainder % 3 == 0)
|
||||
remainder /= 3;
|
||||
while (remainder % 5 == 0)
|
||||
remainder /= 5;
|
||||
|
||||
// I love types
|
||||
long sqrtOfRemainder = (long)sqrt((double)remainder);
|
||||
|
||||
for (long i = 7; i < sqrtOfRemainder; i++)
|
||||
{
|
||||
if (number % i == 0 && isPrime(i))
|
||||
{
|
||||
printf("Prime factor found: %ld\n", i);
|
||||
largestPrimeFactor = i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("The largest prime factor of %ld is %ld\n", number, largestPrimeFactor);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// This program is specific to the given problem where only 3-digit numbers are
|
||||
// considered. I _could_ have made it generalizable... but I feel lazy.
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int largestPalindrome = 0;
|
||||
char buffer[6]; // 999x999 gives a 6-digit number
|
||||
char revbuffer[3];
|
||||
|
||||
for (int a = 100; a < 1000; a++)
|
||||
{
|
||||
for (int b = 100; b < 1000; b++)
|
||||
{
|
||||
int product = a * b;
|
||||
sprintf(buffer, "%d", product);
|
||||
revbuffer[0] = buffer[5];
|
||||
revbuffer[1] = buffer[4];
|
||||
revbuffer[2] = buffer[3];
|
||||
if (strncmp(buffer, revbuffer, 3) == 0)
|
||||
{
|
||||
printf("%d * %d = %d is palindromic\n", a, b, product);
|
||||
if (product > largestPalindrome)
|
||||
largestPalindrome = product;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("The largest palindrome made from the product of two 3-digit numbers is %d\n", largestPalindrome);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
for (int i = 1; i < 2147483647; i++)
|
||||
{
|
||||
for (int j = 1; j < 21; j++)
|
||||
{
|
||||
if (i % j != 0)
|
||||
break;
|
||||
if (j == 20)
|
||||
{
|
||||
printf("The sallest positive number that is evenly divisible by all of the numbers from 1 to 20 is: %d\n", i);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int difference = 0;
|
||||
int squareOfSum = 0;
|
||||
int sumOfSquares = 0;
|
||||
for (int i = 1; i < 101; i++)
|
||||
{
|
||||
squareOfSum += i;
|
||||
sumOfSquares += i*i;
|
||||
}
|
||||
squareOfSum *= squareOfSum;
|
||||
difference = squareOfSum - sumOfSquares;
|
||||
printf("The difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is: %d\n", difference);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
static int
|
||||
isPrime(long number)
|
||||
{
|
||||
if (number <= 1)
|
||||
return 0;
|
||||
for (long i = 2; i * i <= number; i++)
|
||||
if (number % i == 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
int primeCount = 0;
|
||||
for(long i = 0; i < LONG_MAX; i++)
|
||||
{
|
||||
if (isPrime(i))
|
||||
{
|
||||
primeCount++;
|
||||
if (primeCount == 10001)
|
||||
{
|
||||
printf("The 10,001st prime is %ld\n", i);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
char* thousandDigitNumber = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
|
||||
long largestProduct = 0;
|
||||
|
||||
for (int i = 0; i < 988; i++)
|
||||
{
|
||||
long product = 1;
|
||||
for (int j = i; j < i+13; j++) {
|
||||
product *= (thousandDigitNumber[j]-'0');
|
||||
}
|
||||
if (product > largestProduct)
|
||||
largestProduct = product;
|
||||
}
|
||||
|
||||
printf("The value of the largest product of thirteen adjacent digits in the thousand digit number is: %ld\n", largestProduct);
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// This is definitely a silly and innefficient way to do this
|
||||
// But... everything is fast for small n :^)
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
for (int a = 1; a < 1000; a++)
|
||||
{
|
||||
for (int b = 1; b < 1000; b++)
|
||||
{
|
||||
for (int c = 1; c < 1000; c++)
|
||||
{
|
||||
if (a + b + c != 1000)
|
||||
continue;
|
||||
if (a*a + b*b != c*c)
|
||||
continue;
|
||||
printf("Solution found:\n");
|
||||
printf("a = %d\n", a);
|
||||
printf("b = %d\n", b);
|
||||
printf("c = %d\n", c);
|
||||
printf("Product of a, b, and c: %d\n", a*b*c);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Solution not found :(\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
Loading…
Reference in New Issue