-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicClassification.c
66 lines (58 loc) · 1.3 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
#include "NumClass.h"
#define EPS 0.1 // Epsilon is 10^-1
// Declaring helper functions
int getFactorial(int);
int getSqrt(int);
double getAbs(double);
int isStrong(int num){
int digit, sum = 0;
int originNum = num;
while(num > 0){
digit = num%10;
sum += getFactorial(digit);
num /= 10;
}
return (sum == originNum) ? 1 : 0;
}
int isPrime(int num){
if(num == 1 || num == 2){
return 1;
}
int i=2;
while(i<=getSqrt(num)){
if(num%i == 0){
return 0;
}
i++;
}
return 1;
}
int getFactorial(int num) {
int factorial = 1;
for(int i=1;i<=num;i++){
factorial *= i;
}
return factorial;
}
/* Using Heron's method to calculate the square root of a number
sqrt(n+1)=(sqrt(n)+(num/sqrt(n)))*0.5
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
*/
int getSqrt(int num){
double sqrt_i = 1; // square root of 1
while (getAbs((sqrt_i*sqrt_i) - num) > EPS){
sqrt_i = (sqrt_i+(num/sqrt_i))*0.5;
}
return (int)sqrt_i;
}
/* Returns the number, in power of pow*/
int getPow(int num, int pow) {
int result = 1;
for(int i = 1; i <= pow; i++){
result *= num;
}
return result;
}
double getAbs(double num){
return (num < 0) ? -num : num;
}