-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedClassificationRecursion.c
80 lines (69 loc) · 1.11 KB
/
advancedClassificationRecursion.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
#include <stdio.h>
#include "NumClass.h"
//calc x^pow
int Pow(int x, int pow){
if(pow == 0){
return 1;
}
return x*Pow(x,pow-1);
}
int digAmount(int x){
if(x==0){
return 1;
}
int sum = 0;
while(x>0){
x = x/10;
sum +=1;
}
return sum;
}
int isrec(int x,int digits){
int sum = 0;
if(digits == 1){
return true;
}
if (x==0){
return 0;
}
sum = isrec(x/10,digits) + Pow(x%10,digits);
if(digAmount(x) == digits){
if(sum == x){
return true;
}
return false;
}
return sum;
}
int isArmstrong(int x){
x = x+0;
return isrec(x,digAmount(x));
}
int getDig(int x, int i){
int d = x/Pow(10,i);
return d%10;
}
int isrecP(int x,int times){
int digits = digAmount(x);
int i = times;
if(times>=digits/2){
return true;
}
if(getDig(x,i) != getDig(x,digits-1-i)){
return false;
}
return isrecP(x,times+1);
}
int isPalindrome(int x){
return isrecP(x,0);
}
/*
int main()
{
for(int i = 0; i < 1112; i+=1){
printf("%d: is palindrome? %d \n" ,i,isPalindrome(i));
printf("%d: is armstrong? %d \n" ,i,isArmstrong(i));
printf("------------------------\n");
}
return 0;
}*/