Skip to content

Instantly share code, notes, and snippets.

@connors511
Created October 27, 2011 06:00
Show Gist options
  • Save connors511/1318879 to your computer and use it in GitHub Desktop.
Save connors511/1318879 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define LENGTH(x) (sizeof(x)/sizeof(*(x)))
//static int[] numbers = { 49, 82, 96, 21, 81, 38, 7, 79, 32, 39, 94, 74, 70, 45, 8, 25, 86, 48, 85, 14, 6, 18, 48, 76, 2, 79, 17, 68, 62, 20 };
//static int[] numbers = { 7678, 3701, 2012, 7339, 6890, 1413, 2878, 3109, 2520, 579, 5047, 1562, 2986, 7638, 5555 };
//static int[] numbers = { 3, 1, 4, 3 };
typedef struct {
char *name;
int number;
//struct DATA* num1;
int num2;
} DATA;
DATA *duplicates = NULL;
int num_elements = 0; // To keep track of the number of elements used
int num_allocated = 0; // This is essentially how large the array is
int AddToArray (DATA item)
{
void *_tmp;
if(num_elements == num_allocated) { // Are more refs required?
// Feel free to change the initial number of refs and the rate at which refs are allocated.
if (num_allocated == 0)
num_allocated = 3; // Start off with 3 refs
else
num_allocated *= 2; // Double the number of refs allocated
// Make the reallocation transactional by using a temporary variable first
_tmp = realloc(duplicates, (num_allocated * sizeof(DATA)));
// If the reallocation didn't go so well, inform the user and bail out
if (!_tmp)
{
fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
return(-1);
}
// Things are looking good so far, so let's set the
duplicates = (DATA*)_tmp;
}
duplicates[num_elements] = item;
num_elements++;
return num_elements;
}
DATA *duplicates2 = NULL;
int num_elements2 = 0; // To keep track of the number of elements used
int num_allocated2 = 0; // This is essentially how large the array is
int AddToArray2 (DATA item)
{
void *_tmp;
if(num_elements2 == num_allocated2) { // Are more refs required?
// Feel free to change the initial number of refs and the rate at which refs are allocated.
if (num_allocated2 == 0)
num_allocated2 = 3; // Start off with 3 refs
else
num_allocated2 *= 2; // Double the number of refs allocated
// Make the reallocation transactional by using a temporary variable first
_tmp = realloc(duplicates2, (num_allocated2 * sizeof(DATA)));
// If the reallocation didn't go so well, inform the user and bail out
if (!_tmp)
{
fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
return(-1);
}
// Things are looking good so far, so let's set the
duplicates2 = (DATA*)_tmp;
}
duplicates2[num_elements2] = item;
num_elements2++;
return num_elements2;
}
int inArray(DATA* item)
{
int i;
for(i = 0; i < num_elements; i++)
{
if (item->number == duplicates[i].number)
return 1;
}
return 0;
}
int sort(const void *x, const void *y) {
return (*(int*)x - *(int*)y);
}
//int numbers[] = { 3, 1, 4, 3 };
//int numbers[] = {7678, 3701, 2012, 7339, 6890, 1413, 2878, 3109, 2520, 579, 5047, 1562, 2986, 7638, 5555};
int wall = -1;
int numbers[] = { 49, 82, 96, 21, 81, 38, 7, 79, 32, 39, 94, 74, 70, 45, 8, 25, 86, 48, 85, 14, 6, 18, 48, 76, 2, 79, 17, 68, 62, 20 };
int divide(int base, int start)
{
DATA tmp;
int i;
for (i = start + 1; i < LENGTH(numbers); i++)
{
tmp.number = base + numbers[i];
if(!inArray(&tmp))
{
if (AddToArray(tmp) == -1)
printf("Failed to add %d\n", numbers[i]);
} else {
tmp.number = i;
AddToArray2(tmp);
if (wall == -1 || i < wall)
wall = i;
}
divide(tmp.number, i);
}
}
int dupeCount = 0;
void calculate()
{
int i, j, c, k;
DATA tmp;
tmp.number = numbers[0];
//tmp.num1 = NULL;
tmp.num2 = -1;
AddToArray(tmp);
for(i = 1; i < LENGTH(numbers); i++)
{
c = num_elements;
for(j = 0; j < c; j++) {
tmp.number = numbers[i] + duplicates[j].number;
//tmp.num1 = &duplicates[j];
tmp.num2 = numbers[i];
if (!inArray(&tmp)) {
AddToArray(tmp);
} else {
dupeCount++;
AddToArray2(tmp);
}
}
tmp.number = numbers[i];
//tmp.num1 = NULL;
tmp.num2 = -1;
if (!inArray(&tmp)) {
AddToArray(tmp);
} else {
dupeCount++;
AddToArray2(tmp);
}
}
}
int getBase(DATA* data)
{
/*if (data->num1 != NULL) {
return getBase(data);
} else {
return data->num2;
}*/
}
int main()
{
char input[80];
int i, x;
time_t start, stop;
clock_t ticks;
/*time(&start);
//qsort(numbers, sizeof(numbers)/sizeof(int), sizeof(int), sort);
x = divide(0, -1);
time(&stop);
ticks = clock();
//for(i = 0; i < num_elements; i++)
//{
// printf("%d, ", duplicates[i].number);
//}
printf("\n");
printf("Combos: %d\n", num_elements);
printf("Used %0.3f seconds of CPU time. \n", (double)ticks/CLOCKS_PER_SEC);
printf("Returned number: %d\n", x);
printf("Duplicates: %d\n", numbers[wall]);
qsort(duplicates, sizeof(duplicates)/sizeof(int), sizeof(int), sort);
/*for(i = 0; i < num_elements; i++)
{
printf("%d, ", duplicates[i].number);
}*/
printf("\n\n");
duplicates = NULL;
num_elements = 0;
num_allocated = 0;
time(&start);
calculate();
time(&stop);
ticks = clock();
printf("Combos: %d\n", num_elements);
printf("Used %0.3f seconds of CPU time. \n", (double)ticks/CLOCKS_PER_SEC);
printf("Dupe count: %d\n", dupeCount);
qsort(duplicates2, sizeof(duplicates2)/sizeof(int), sizeof(int), sort);
printf("First dupe: %d\n", duplicates2[0].num2);
/*for(i = 0; i < num_elements; i++)
{
printf("%d, ", duplicates[i].number);
}*/
/*for(i = 0; i < num_elements2; i++)
{
printf("%d, ", duplicates2[i].number);
}*/
gets(input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment