Python While 循环教程 #Day3

霞舞
发布: 2024-12-31 11:15:11
原创
738人浏览过

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.

立即学习Python免费学习笔记(深入)”;

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的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号