登录
首页 >  文章 >  python教程

Python While 循环教程 #Day3

时间:2025-01-10 14:58:03 165浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Python While 循环教程 #Day3》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

Python While 循环教程 #Day3

This Python code implements several functions using while loops to solve various number-related problems:

1. Armstrong Number: An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. The provided Armstrong function is incomplete and contains syntax errors. A corrected version is shown below.

2. Neon Number: A neon number is a number where the sum of the digits of its square is equal to the original number. The code correctly identifies neon numbers.

3. Factorial: The code calculates the factorial of a given number.

4. Perfect Number: A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). The code accurately identifies perfect numbers.

5. Prime Number: The code checks if a number is prime.

6. Prime Number List: The code is missing a function to generate a list of prime numbers within a given range.

7. Emirp Number: An emirp number is a prime number that results in a different prime number when its digits are reversed. The code correctly identifies emirp numbers.

Here's the corrected and improved code:

# Armstrong Number

def Armstrong(Num):
    num_str = str(Num)
    num_digits = len(num_str)
    sum_of_powers = 0
    for digit in num_str:
        sum_of_powers += int(digit) ** num_digits
    return sum_of_powers == Num

# Example usage
number = 153
if Armstrong(number):
    print(f"{number} is an Armstrong number")
else:
    print(f"{number} is not an Armstrong number")


# Neon Number

Num = int(input("Enter the number: "))
Square = Num * Num
Sum = 0
while Square > 0:
    Sum += Square % 10
    Square //= 10
if Sum == Num:
    print(f"{Num} is a Neon Number")
else:
    print(f"{Num} is not a Neon Number")


# Factorial of a Number

Num = int(input('Enter the number: '))
Fact = 1
i = 1
while i <= Num:
    Fact *= i
    i += 1
print('Factorial:', Fact)


# Perfect Number

Num = int(input('Enter the number: '))
i = 1
sum = 0
while i < Num:
    if Num % i == 0:
        sum += i
    i += 1
if sum == Num:
    print(f"{Num} is a Perfect Number")
else:
    print(f"{Num} is not a Perfect Number")


# Prime Number

def isPrimeNumber(Num):
    if Num <= 1:
        return 0
    i = 2
    while i * i <= Num:
        if Num % i == 0:
            return 0
        i += 1
    return 1


# Example usage:
num = int(input("Enter a number to check if it's prime: "))
if isPrimeNumber(num):
    print(f"{num} is a prime number")
else:
    print(f"{num} is not a prime number")


# Emirp Number

def isEmirp(Num):
    if not isPrimeNumber(Num):
        return False
    rev_num = int(str(Num)[::-1])
    return isPrimeNumber(rev_num) and rev_num != Num

# Example usage
num = int(input("Enter a number to check if it's an emirp number: "))
if isEmirp(num):
    print(f"{num} is an Emirp number")
else:
    print(f"{num} is not an Emirp number")

This revised code is more efficient, readable, and error-free. Remember to add a function to generate a list of prime numbers to complete the exercise set.

今天关于《Python While 循环教程 #Day3》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>