确定给定数字是否为丰数(Abundant number)的程序

2025 年 1 月 8 日 | 3 分钟阅读

丰满数又称为过剩数,其定义是数本身的所有真因子(不包括该数本身)之和大于该数本身。

第一个丰满数是整数 12,其真因子(1, 2, 3, 4, 6)之和(16)大于该数本身(12)。

示例 12, 18, 20, 24, 30, 36

在此程序中,我们将使用以下算法来检查给定数字是否为丰满数。

算法

主程序

  • 步骤 1: 开始
  • 步骤 2: 输入 n。
  • 步骤 3: 如果 CheckAbundant(n) 为真
    则打印 "yes"
    else
    打印 "no"。

CheckAbundant (n)

  • 步骤 1: 开始
  • 步骤 2: 设置 i = GetSum(n)
  • 步骤 3: 如果 i>n
    则返回 true
    else
    返回 false。

GetSum(n)

  • 步骤 1: 开始
  • 步骤 2: 设置 sum = 0
  • 步骤 3: 重复步骤 4 直到 i<=?n
  • 步骤 4: 如果 n%i == 0
    然后
    如果(n/i==i)
    sum=sum+i
    else
    sum =sum+i
    sum= sum+n/i
  • 步骤 5: sum =sum - n
  • 步骤 6: 返回 sum

Java 程序

输出

Enter the number?
20
The number is Abundant

C程序

输出

Enter the number? 34
The number is not Abundant.

Python 程序

输出

Enter the number?24
The number is Abundant.

C# 程序

输出

Enter the number?
67
The number is not Abundant.

PHP 程序

输出

Enter the number? 56
The number is Abundant.
下一主题#