-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q10_FancyNumber.java
38 lines (34 loc) · 1.12 KB
/
Q10_FancyNumber.java
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
public class Q10_FancyNumber {
public static void main(String[] args) {
//These numbers are fancy numbers 3*17 + 2*5 =61 :- {1,1,5,17,61,217,....}
System.out.println("Is Fancy Number?");
System.out.println(IsFancyNumberMethod(6));
System.out.println(IsFancyNumberMethod(17));
System.out.println(IsFancyNumberMethod(61));
System.out.println(IsFancyNumberMethod(217));
}
public static int IsFancyNumberMethod(int num) {
if(num==1){
return 1;
}
if(num<=0){
return 0;
}
int isFancy=0;
int previous=1;
int pprevious=1;
int current=0;
for(int i=0;i<num/2;i++){
current= 3*previous+2*pprevious;
if(current==num){
isFancy=1;
break;
}else{
pprevious=previous;
previous=current;
isFancy=0;
}
}
return isFancy;
}
}