-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimeFactorization.c
More file actions
58 lines (47 loc) · 1.15 KB
/
primeFactorization.c
File metadata and controls
58 lines (47 loc) · 1.15 KB
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
/**
* @file primeFactorDecomposition.c
* @brief Outputs what prime numbers multiply together to make the argv[1] number.
* @author Pedro Henrique Pinto de Oliveira
* @date 2023-12-12
*/
/* Inclusions */
#include "headers/errors.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
/* Constants */
#define TRUE (1 == 1)
#define FALSE (!TRUE)
/* Types */
typedef uint8_t bool_t;
/* Functions */
bool_t isPrime(unsigned long n) {
int iterator = n;
if(n != 1 && n != 2) {
if(n % --iterator == 0) return FALSE;
}
return TRUE;
}
void DecomposePrimeFactors(unsigned long n) {
unsigned long prime = 2;
printf("%lu", n);
while(n != 1) {
if(n % prime == 0) {
n /= prime;
printf(" : %lu = %lu", prime, n);
} else {
do {
prime++;
} while (isPrime(prime) == FALSE);
}
}
puts("");
}
int main(int argc, char ** argv) {
unsigned long num;
if(argc != NO_ARGS_PROVIDED + 1) return NOT_ENOUGH_ARGS;
num = strtoul(argv[1], NULL, 0);
DecomposePrimeFactors(num);
return SUCCESS;
}