Euler krams

This commit is contained in:
madmaurice 2015-11-22 18:19:34 +01:00
parent 511df339b5
commit 49e072e29e
4 changed files with 107 additions and 0 deletions

13
euler1.c Normal file
View file

@ -0,0 +1,13 @@
#include <stdio.h>
int numbersum(int i, int p) {
int n = p/i;
return i*n*(n+1)/2;
}
int main( int argc, char *argv[] ) {
int n = 999;
int s = numbersum(3,n) + numbersum(5,n) - numbersum(15,n);
printf("%d\n",s);
return 0;
}

24
euler3.c Normal file
View file

@ -0,0 +1,24 @@
#include <stdio.h>
#include <math.h>
#include <assert.h>
long max(long a, long b) {
return a > b ? a : b;
}
long largest_prime_factor(long n) {
int p = 2;
while(n > 1) {
while(p < n && n%p!=0) p++;
while(n%p==0) n/=p;
}
return p;
}
int main( int argc, char *argv[] ) {
assert(largest_prime_factor(13195) == 29);
printf("%d\n",largest_prime_factor(600851475143));
return 0;
}

49
euler4.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
struct palindrome_info {
int a;
int b;
int palindrome;
};
int makepalindrome(int n) {
int p = n*1000;
p += 100 * (n%10);
p += 10 * ((n/10)%10);
p += (n/100)%10;
return p;
}
struct palindrome_info* largestpalindrome() {
struct palindrome_info* info = malloc(sizeof(struct palindrome_info));
info->a = 0;
info->b = 0;
info->palindrome = 0;
int p,a;
for(int i = 999; i > 0; i--) {
p = makepalindrome(i);
a = 999;
while(a > 0 && p%a != 0) a--;
if(a > 0 && p/a < 1000) {
info->palindrome = p;
info->a = a;
info->b = p/a;
return info;
}
}
free(info);
return NULL;
}
int main( int argc, char *argv[] ) {
struct palindrome_info* info = largestpalindrome();
if(info != NULL) {
printf("A: %d\n",info->a);
printf("B: %d\n",info->b);
printf("Palindrom: %d\n",info->palindrome);
free(info);
}
return 0;
}

21
euler5.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
int ggT(int a, int b) {
if(a==0) return b;
while(b!=0) {
if(a > b) a -= b;
else b -= a;
}
return a;
}
int main( int argc, char *argv[] ) {
int n = 1;
for(int i = 20; i > 1; i--) {
if(n%i!=0) n*=i/ggT(i,n);
}
printf("%d\n",n);
return 0;
}