-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicClassification.c
72 lines (61 loc) · 1.39 KB
/
basicClassification.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
#include <stdio.h>
#include <math.h>
#include "NumClass.h"
/*declaraion for factorial function*/
int factorial(int num);
int isStrong(int num){
int sum =0;
int checkLength = num;
int length =0;
/* this loop checks the length of the number to know the time to run to loop later */
while(checkLength!=0)
{
checkLength = checkLength/10;
length++;
}
int checkSum = num;
/* this loop seperates the digits and sums the factorial of each one */
for(int i =0; i <length;i++)
{
int digit = num%10;
num = num/10;
sum += factorial(digit);
}
if(sum == checkSum)
{
return TRUE;
}
return FALSE;
}
int factorial(int num){
if(num ==0)
{
return 1;
}
if(num ==1)
{
return 1;
}
return num*factorial(num-1);
}
int isPrime(int num){
if(num == 2 || num == 1)
{
return TRUE;
}
//if number is lower then 1 it cant be prime
if(num <1)
{
return FALSE;
}
// use math sqrt function to get the sqare root
double sqrtNum = sqrt(num);
/* run until the sqare root and check if any number higher then 2 divides the number perfectly
if so, it returns false, if it doesnt, it will return true */
for(int i = 2; i <= sqrtNum;i++){
if(num%i == 0){
return FALSE;
}
}
return TRUE;
}