To find whether a number is a **Lucas** number:

Step 1: Add the squares of each digit of the number.

Step 2: If the sum is not a single digit number repeat step 1.

Step 3: If the sum of the squares of each digit is a single digit and is equal to 1 then it is Lucas number.

else it is not a **Lucas** Number.

Example:

Input: 86

86 –> 8*8+6*6 = 100

100 –> 1*1+0*0+0*0 = 1

Then 86 is a **Lucas** number

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 |
import java.io.*; public class Lucas { public int sum(int n) { int s=0, d; while(n!=0) { d = n%10; s = s+d*d; n = n/10; } return s; } boolean isLucas(int n) { while(n>9) { n = sum(n); } if(n==1) return true; return false; } public static void main()throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int x; System.out.println("Enter a number:"); x = Integer.parseInt(r.readLine()); Lucas obb = new Lucas(); System.out.println("All Lucas Numbers are:"); for(int i=1;i<=x;i++) { if(obb.isLucas(i)) System.out.println(i); } } } |

## Leave a Reply