some c solutions

master
Evan Hemsley 2016-09-02 16:58:42 -07:00
parent 1b114ac272
commit a15137d2bb
8 changed files with 151 additions and 0 deletions

BIN
c/binaries/euler001 Executable file

Binary file not shown.

5
c/euler.c Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
void print_result(int result) {
printf("%d\n", result);
}

1
c/euler.h Normal file
View File

@ -0,0 +1 @@
void print_result(int result);

14
c/euler001.c Normal file
View File

@ -0,0 +1,14 @@
#include "euler.h"
int main()
{
int sum = 0;
for (int i = 0; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
print_result(sum);
return 0;
}

BIN
c/euler002 Executable file

Binary file not shown.

29
c/euler002.c Normal file
View File

@ -0,0 +1,29 @@
#include "euler.h"
#include "int_list.h"
#include <stdio.h>
int_list *fibonacci_until(int num)
{
static int_list list;
int_list_init(&list);
int term_one = 1;
int term_two = 2;
while (term_one < num)
{
if (term_one % 2 == 0) { int_list_add(&list, term_one); }
int temp = term_two;
term_two = term_two + term_one;
term_one = temp;
}
return &list;
}
int main()
{
int_list *result;
result = fibonacci_until(4000000);
print_result(int_list_sum(result));
return 0;
}

82
c/int_list.c Normal file
View File

@ -0,0 +1,82 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "int_list.h"
void int_list_init(int_list *l) {
l->data = NULL;
l->size = 0;
l->count = 0;
}
int int_list_count(int_list *l) {
return l->count;
}
void int_list_add(int_list *l, int value) {
if (l->size == 0) {
l->size = 10;
l->data = malloc(sizeof(int*) * l->size);
memset(l->data, '\0', sizeof(int*) * l->size);
}
if (l->size == l->count) {
l->size *= 2;
l->data = realloc(l->data, sizeof(int*) * l->size);
}
l->data[l->count] = value;
l->count++;
}
int int_list_get(int_list *l, int index) {
if (index >= l->count) {
printf("error: index out of range");
return 0;
}
return l->data[index];
}
void int_list_set(int_list *l, int index, int value) {
if (index >= l->count) {
printf("error: index out of range");
return;
}
l->data[index] = value;
}
void int_list_delete(int_list *l, int index) {
if (index >= l->count) {
printf("error: index out of range");
return;
}
int j = index;
for (int i = index; i < l->count; i++) {
l->data[j] = l->data[i];
j++;
}
l->count--;
}
int int_list_sum(int_list *l) {
int sum = 0;
for (int i = 0; i < l->count; i++) {
sum += (l->data[i]);
}
return sum;
}
void int_list_print(int_list *l) {
for (int i = 0; i < l->count; i++) {
printf("%d ", (l->data[i]));
}
printf("\n");
}
void int_list_free(int_list *l) {
free(l->data);
}

20
c/int_list.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef INT_LIST_H_
#define INT_LIST_H_
typedef struct int_list_ {
int* data;
int size;
int count;
} int_list;
void int_list_init(int_list *l);
int int_list_count(int_list *l);
void int_list_add(int_list *l, int value);
int int_list_get(int_list *l, int index);
void int_list_set(int_list*l, int index, int value);
void int_list_delete(int_list *l, int index);
int int_list_sum(int_list *l);
void int_list_print(int_list *l);
void int_list_free(int_list *l);
#endif