Find Cute Numbers with Python

python day 22

Today we will introduce two kinds of interesting number: Narcissistic Number, Perfect Number, and Prime Number.

  • Narcissistic Number
numbers that can be represented as the sum of the cube of each digits.

$$153= 1^{3} + 5^{3} +3^{3}$$

def nar():
    for x in range (1,1001):
        s=x%10
        t=x//10%10
        h=x//100
        if x==s**3+t**3+h**3:
            print("%d is a narcissistic number" %x)
nar()
1 is a narcissistic number
153 is a narcissistic number
370 is a narcissistic number
371 is a narcissistic number
407 is a narcissistic number
1000 is a narcissistic number
  • Perfect Number

the number is equal to its sum of proper divisors. $$ 6 = 1+ 2+ 3$$

from math import sqrt
def perfect(m):
    for i in range (1,m):
        s=0
        for factor in range (1, int(sqrt(i))+1):
            if i % factor==0:
                s +=factor
                if factor >1 and i//factor != factor:
                    s+= i//factor
        if s==i:
            print(i,"is a perfect number" )
    print("So you are perfect2!")
perfect(10000)
1 is a perfect number
6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number
So you are perfect2!

Once, i thought l is 8128, but now i know, that’s bc he is in C. everything about him has an “i” attached to it.
I just didnt notice/face it back then.
I think i like ppl in R! Keep Real!
就像 Yibo! 喜欢,不喜欢都说出来!
有什么大不了的!

  • Prime Number

The only divisor of the number is 1 and itself

from math import sqrt
def prime(m):
    for i in range (2,m):
        isprime=True
        for factor in range (2, int(sqrt(i))+1):
            if i % factor ==0:
                isprime=False
                break
        if isprime:
            print(i)
prime(100)
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Story time:

Narcissistic flower is daffodil, a handsome boy fell in love with the beauty of himself when he saw himself in the lake.
he became a daffodil so he can look at himself all day long!

Happy studying!

Ref:

https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/05.%E6%9E%84%E9%80%A0%E7%A8%8B%E5%BA%8F%E9%80%BB%E8%BE%91.md

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: