Created
November 29, 2011 19:46
-
-
Save AndreaCrotti/1406149 to your computer and use it in GitHub Desktop.
Solution of euler problem 1 in C, without using mutable structures
This file contains hidden or 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
| /** | |
| Euler problem number 1 | |
| */ | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #define MAX 1000 | |
| int to_sum[MAX]; | |
| int is_multiple_of(int num) { | |
| return ((num % 3 == 0) || (num % 5 == 0)); | |
| } | |
| void fill_to_sum(int size) { | |
| to_sum[size] = size; | |
| if (size == 0) { | |
| return; | |
| } else { | |
| fill_to_sum(size-1); | |
| } | |
| } | |
| int filter_on_multiples(int size, int *list, int totsum) { | |
| if (size == 0) { | |
| return totsum; | |
| } else { | |
| if (is_multiple_of(list[size])) { | |
| printf("adding %d\n", list[size]); | |
| return list[size] + filter_on_multiples(size-1, list, totsum); | |
| } else { | |
| return filter_on_multiples(size-1, list, totsum); | |
| } | |
| } | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| fill_to_sum(1000); | |
| printf("result = %d\n", filter_on_multiples(MAX-1, to_sum, 0)); | |
| return 0; | |
| } | |
| /* Local Variables: */ | |
| /* compile-command: "gcc problem1.c -o problem1" */ | |
| /* End: */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment