forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triautomorphic_number.java
79 lines (68 loc) · 1.6 KB
/
triautomorphic_number.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
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
/*
* A number n is called triautomorphic if 3n^2 ends in n.
* Example:
* n=667
* 3n^2 = 1334667
* which ends with 667
* This code checks whehter the input number is triautomorphic or not.
*/
import java.util.*;
public class triautomorphic_number
{
void main()
{
//driver function
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number: ");
int number = scanner.nextInt();
scanner.nextLine();
if(is_triautomorphic(number) == true)
{
System.out.println(number+" is a Triautomorphic number!");
}
else
{
System.out.println(number+" is not a Triautomorphic number!");
}
}
boolean is_triautomorphic(int number)
{
//this function checks whether number is triautomorphic or not
int triauto = 3*number*number;
if((triauto % Math.pow(10,no_of_digits(number))) == number)
{
return true;
}
else
{
return false;
}
}
int no_of_digits(int number)
{
//this function counts the number of digits
int count = 0;
while(number!=0)
{
count++;
number/=10;
}
return count;
}
}
/*
* Test Cases-
* 1.
* Enter number:
* 5
* 5 is a Triautomorphic number!
*
* 2.
* Enter number:
* 8
* 8 is not a Triautomorphic number!
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* where n is the number of digits in the number
*/