Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,562 questions

52,727 answers

573 users

How to check whether a number is magic number in Python

2 Answers

0 votes
# A magic number is a number that repeated the sum of its digits 
# till we get a single digit is equal to 1

# 1729 = 1 + 7 + 2 + 9 = 19 
# 19 = 1 + 9 = 10
# 10 = 1 + 0 = 1

import math

num = 1729
digitCount = int(math.log10(num)) + 1
sumOfDigits = 0

temp = num 

while (digitCount > 1):
  sumOfDigits = 0

  while (temp > 0):
    sumOfDigits += temp % 10
    temp = temp // 10

  temp = sumOfDigits
  print(sumOfDigits)
  
  digitCount = int(math.log10(sumOfDigits)) + 1
   

if (sumOfDigits == 1):
    print("Magic number")
else:
    print("Not a magic number")
    
   
    
    
'''
run:

19
10
1
Magic number

'''

 



answered Oct 26, 2021 by avibootz
edited Oct 30, 2021 by avibootz
0 votes
# A magic number is a number that repeated the sum of its digits 
# till we get a single digit is equal to 1
 
# 1729 = 1 + 7 + 2 + 9 = 19 
# 19 = 1 + 9 = 10
# 10 = 1 + 0 = 1
 
import math

def magicNumber(number):
    if (((number - 1) % 9) == 0):
        return True
    else:
        return False
  
 
num = 1729

if (magicNumber(num)):
    print("Magic number")
else:
    print("Not a magic number")
     
    
     
     
'''
run:
 
Magic number
 
'''

 



answered Oct 30, 2021 by avibootz

Related questions

2 answers 151 views
2 answers 124 views
2 answers 152 views
...