itsme86: Yes, hahahaha. Well at least your snipe taught me something about GLib. I learned that GLib is annoying:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
// Because this is naive, we will just recursively put prime factors in a "bucket"
// This algorithm has the advantage that it's what most of us actually do when WE
// sit down to factorize a number.
void Factorize(int n, GHashTable* factor_list)
{
int i;
for(i = 2; i <= n; i++)
{
if(n % i == 0)
{
// Check if i has been found to be a prime factor yet
gpointer it = g_hash_table_lookup(factor_list, &i);
if(it == NULL) // First time we have found this prime factor
{
int* k = malloc(sizeof(int)); *k = i;
int* v = malloc(sizeof(int)); *v = 1;
g_hash_table_insert(factor_list, k, v);
}
else // We just have to increment the power of this prime factor
{
(*(int*)it)++;
}
Factorize(n / i, factor_list); // Factor the smaller result
return;
}
}
}
void Print(gpointer key, gpointer value, gpointer user_data)
{
printf("%d^%d\n", *(int*)key, *(int*)value);
}
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("Usage: ./foo [NUMBER]\n");
return 0;
}
GHashTable* factor_list = g_hash_table_new_full( g_int_hash, g_int_equal, FreeElement, FreeElement );
Factorize(atoi(argv[1]), factor_list);
g_hash_table_foreach(factor_list, Print, NULL);
g_hash_table_destroy(factor_list);
return 0;
}