Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created November 6, 2012 01:53
Show Gist options
  • Select an option

  • Save chintanparikh/4021994 to your computer and use it in GitHub Desktop.

Select an option

Save chintanparikh/4021994 to your computer and use it in GitHub Desktop.
Something.c
#include <stdio.h>
#include <stdlib.h>
struct node {
unsigned long long int data;
struct node *next;
};
void push(struct node** headRef, unsigned long data)
{
struct node* newNode;
newNode = malloc(sizeof(*newNode));
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
}
struct node* primeFactors(unsigned long num, struct node* head)
{
printf("NUM: %lu\n", num);
if (num == 1)
{
printf("AWWW YEAH");
return head;
}
int factor = 0;
int i = 2;
while (i <= num)
{
if (num % i == 0)
{
factor = i;
break;
}
i++;
}
push(&head, factor);
printf("Factor: %d, Num: %lu\n", factor, num);
return primeFactors(num / factor, head);
}
int main(int argc, char const *argv[])
{
struct node* head = NULL;
primeFactors(600851475143, head);
printf("%llu\n", head->data);
}
@chintanparikh
Copy link
Author

Output:

$ ./3
NUM: 600851475143
Factor: 71, Num: 600851475143
NUM: 8462696833
Factor: 839, Num: 8462696833
NUM: 10086647
Factor: 1471, Num: 10086647
NUM: 6857
Factor: 6857, Num: 6857
NUM: 1
[1] 5106 segmentation fault (core dumped) ./3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment