Project Euler Solution

This commit is contained in:
Aman Raj 2019-10-01 19:51:55 +05:30
parent 8ea6314aa6
commit b25076fb8c
4 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,31 @@
#include <stdio.h>
int is_palindromic(unsigned int n)
{
unsigned int reversed = 0, t = n;
while (t>0)
{
reversed = 10 * reversed + (t % 10);
t /= 10;
}
return reversed == n;
}
int main(void)
{
unsigned int i, j, max = 0;
for (i = 100; i <= 999; i++)
{
for (j = 100; j <= 999; j++)
{
unsigned int p = i * j;
if (is_palindromic(p) && p > max)
{
max = p;
}
}
}
printf("%u\n", max);
return 0;
}

View File

@ -0,0 +1,30 @@
#include <stdio.h>
unsigned long gcd(unsigned long a, unsigned long b) {
unsigned long r;
if (a > b) {
unsigned long t = a;
a = b;
b = t;
}
while (r = a % b) {
a = b;
b = r;
}
return b;
}
unsigned long lcm(unsigned long a, unsigned long b) {
unsigned long long p = (unsigned long long)a * b;
return p / gcd(a, b);
}
int main(void) {
unsigned long ans = 1;
unsigned long i;
for (i = 1; i <= 20; i++) {
ans = lcm(ans, i);
}
printf("%lu\n", ans);
return 0;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
int main(void) {
unsigned s1 = 0, s2 = 0, i;
for (i = 1; i <= 100; i++) {
s1 += i * i;
s2 += i;
}
unsigned ans = s2 * s2 - s1;
printf("%u\n", ans);
return 0;
}

View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char *sieve;
size_t i;
unsigned count = 0;
size_t n = 1000000;
const unsigned target = 10001;
sieve = calloc(n, sizeof *sieve);
for (i = 2; i < n; i++) {
if (!sieve[i]) {
size_t j;
count++;
if (count == target) {
printf("%lu\n", i);
break;
}
for (j = i * 2; j < n; j += i) {
sieve[j] = 1;
}
}
}
free(sieve);
return 0;
}