-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
81 lines (78 loc) · 2.16 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <primes.h>
#include <stdio.h>
#include <string.h>
#define queue_size 100
int main(int argc, char const *argv[])
{
if (argc > 1)
{
u64 total = strtoull(argv[1], NULL, 10);
long long input = strtoll(argv[1], NULL, 10);
if (input <= 0) //Check if input is in the correct format
{
fprintf(stderr, "Number is 0, negative or in the wrong format (-1)\n");
exit(-1);
}
if (total == 0xFFFFFFFFFFFFFFFF && (strcmp(argv[1], "18446744073709551615") != 0))
{
fprintf(stderr, "Number is too big (-2)\n");
exit(-2);
}
if (total == 1)
{
fprintf(stderr, "Number is 1 (-3)\n");
exit(-3);
}
if (is_prime(total))
{
fprintf(stderr, "Number is prime (-4)\n");
exit(-4);
}
u64* queue=get_prime_fact_64(total, queue_size);
if (*queue == 0 || queue == NULL)
{
fprintf(stderr, "Error while parsing number (-6).\n");
exit(-6);
}
printf("{ ");
u64 acc = 1;
for (int i = 0; i<queue_size; i++) //Print the prime factors in a tidier way;
{
if (queue[i]!=0)
{
if (i>0 && queue[i]==queue[i-1])
acc++;
else
{
if (i>0)
printf(" × ");
printf("%llu", queue[i]);
}
if (queue[i]!=queue[i+1] && acc>1)
{
printf("^%llu", acc);
acc = 1;
}
}
else
break;
}
printf(" }");
// > Comment/uncomment to disable/enable further checking
/**/
printf(" = ");
acc = 1;
for (int i = 0; i < queue_size && queue[i] != 0; i++)
acc *= queue[i];
printf("%llu == %llu", acc, total);
printf("\n");
free(queue);
exit(0);
/**/
}
else
{
fprintf(stderr, "No number provided (-5)\n");
exit(-5);
}
}