-
-
Save sheeit/68c101a49a30823106b3504cb34b0263 to your computer and use it in GitHub Desktop.
Euler Project (in C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016 strupo | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
/* Euler Project - Problem 1 */ | |
int main (void) | |
{ | |
int i; /* counter */ | |
int start = 0; /* start summing from here */ | |
int stop = 1e3; /* stop summing when you reach this */ | |
int mult[2]; /* this array contains the numbers whose multiples will be summed */ | |
long sum = 0L; /* will contain the sum; initialized to zero */ | |
mult[0] = 3; | |
mult[1] = 5; | |
for (i = start; i < stop; ++i) { | |
if (!(i % mult[0] && i % mult[1])) { /* :DDD */ | |
sum += (long) i; | |
} | |
} | |
printf("Sum of multiples of %d and %d from %d to %d = %ld\n", | |
mult[0], mult[1], start, stop, sum); | |
return 0; | |
} | |
/* vim: set et sts=4 sw=4 ts=8 cin fenc=ascii : */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016 strupo | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
/* Euler Project - Problem 2 */ | |
const long int *fibonacci(const long int max); | |
int main (void) | |
{ | |
printf("Sum = %ld\n", *(fibonacci(4e6))); | |
return 0; | |
} | |
const long int *fibonacci(const long int max) | |
{ | |
static long int sum = 0; | |
int i, i1, i2; | |
#ifdef DEBUG | |
int c; | |
c = 0; | |
printf("fib[%*d]\t=\t%*d\n", 4, c++, 6, 0); | |
#endif /* DEBUG */ | |
i2 = 0; | |
i1 = 1; | |
i = 0; | |
while (i < max) { | |
i2 = i1; | |
i1 = i; | |
i = i1 + i2; | |
#ifdef DEBUG | |
printf("fib[%*d]\t=\t%*d\n", 4, c, 6, i); | |
++c; | |
#endif /* DEBUG */ | |
if (!(i % 2)) { | |
sum += i; | |
} | |
} | |
return (const long int *) ∑ | |
} | |
/* vim: set et sts=4 sw=4 ts=8 cin fenc=ascii : */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016 strupo | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
/* Project Euler - Problem 3 */ | |
int is_prime(unsigned long n); | |
int main (void) | |
{ | |
unsigned long num = 600851475143UL; | |
unsigned long i; | |
unsigned long max; | |
max = 1; | |
for (i = 2; i <= floor(sqrt((double) num)); ++i) { | |
if (is_prime(i) && !(num % i)) { | |
max = i; | |
} | |
} | |
printf("Largest prime factor for %ld = %lu\n", num, max); | |
return 0; | |
} | |
int is_prime(unsigned long n) | |
{ | |
unsigned long i; | |
int p; /* p != 0 <=> n is not prime */ | |
p = 1; | |
for (i = 2; i <= floor(sqrt(n)); ++i) { | |
if (!(n % i)) { | |
p = 0; | |
break; | |
} | |
} | |
return p; | |
} | |
/* vim: set et sts=4 sw=4 ts=8 cin fenc=ascii : */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016 strupo | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
#define MAX_STRING 1<<3 | |
/* Project Euler - Problem 4 */ | |
int is_palindrome(const int n); | |
char *int_to_string(const int n); | |
int length_of_string(const char *s); | |
int find_largest_palindrome(void); | |
int main (void) | |
{ | |
int lp; /* largest palindrome */ | |
lp = find_largest_palindrome(); | |
printf("Largest palindrome made from multiplying 3-digit numbers = %d\n", lp); | |
return 0; | |
} | |
int is_palindrome(const int n) | |
{ | |
int p; /* p = 1 <=> n is a palindrome */ | |
int i; /* counter */ | |
char *s; /* string containing n */ | |
int l; /* length of string s */ | |
s = int_to_string(n); | |
l = length_of_string(s) - 1; | |
p = 1; /* Assuming n is a palindrome */ | |
for (i = 0; i <= l / 2; ++i) { | |
if (s[i] != s[l - i]) { | |
p = 0; | |
break; | |
} | |
} | |
return p; | |
} | |
char *int_to_string(const int n) | |
{ | |
int i; /* Number of digits in n */ | |
int l; | |
int m; | |
static char s[MAX_STRING] = {'\0'}; | |
m = n; | |
i = 0; | |
while (m > 0) { | |
s[i] = m % 10 + '0'; | |
m /= 10; | |
++i; | |
} | |
l = i - 1; | |
/* reversing s */ | |
for (i = 0; i < l / 2; ++i) { | |
int tmp; | |
tmp = s[i]; | |
s[i] = s[l - i]; | |
s[l - i] = tmp; | |
} | |
return s; | |
} | |
int length_of_string(const char *s) | |
{ | |
int i; /* counter */ | |
int l; /* length */ | |
l = i = 0; | |
while (s[i++]) { | |
++l; | |
} | |
return l; | |
} | |
int find_largest_palindrome(void) | |
{ | |
int i; /* c u t r */ | |
int j; /* o n e s */ | |
int m; /* max */ | |
m = 0; | |
for (i = 0; i < 1e3; ++i) { | |
for (j = 0; j < 1e3; ++j) { | |
if (is_palindrome(i * j)) { | |
m = i * j > m ? i * j : m; | |
} | |
} | |
} | |
return m; | |
} | |
/* vim: set et sts=4 sw=4 ts=8 cin fenc=ascii : */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment