-
Notifications
You must be signed in to change notification settings - Fork 1k
/
magic_number.jl
58 lines (46 loc) · 1.03 KB
/
magic_number.jl
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
"""Julia program to check if a number is a Magic Number or not
A number is said to be a magic number if the recursive sum of its digits is 1.
For Example Let's Consider
19, 1 + 9 = 10
10, 1 + 0 = 1
= 1 , Hence 19 is a Maigc Number
"""
function sum_of_digits(n)
sum = 0
while(n>0)
rem = n % 10
sum = sum + rem
n = n ÷ 10
end
return sum
end
function check_magic_num(n)
while(n > 9)
n = sum_of_digits(n)
end
if(n == 1)
return true
else
return false
end
end
print("Enter the number: ")
n = readline()
n = parse(Int, n)
res = check_magic_num(abs(n))
if res
println("The given number $n is a Magic Number.")
else
println("The given number $n is not a Magic Number.")
end
"""
Time Complexity: O(log(n)), where 'n' is the given number
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
Enter the number: 23
The given number 23 is not a Magic Number.
SAMPLE 2
Enter the number: 1729
The given number 1729 is a Magic Number..
"""