确定给定数字是否为亏数(Deficient number)的程序

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

亏数(Deficient number)的定义是:一个数的真因数之和小于它本身。

例如,数字 21 的真因数是 (1, 3, 7),它们的和是 (11),小于 21 本身。

算法

主程序

  • 步骤 1: 开始
  • 步骤 2: 输入 n
  • 步骤 3: 如果 Def(n) 为真,则
    打印 "yes"
    else
    打印 "no"
  • 步骤 4:结束

Def(n)

  • 步骤 1: 开始
  • 步骤 2: 设置 i = divsum(n)
  • 步骤 3: 如果 i < 2n
    则返回 true
    else
    返回 false
  • 步骤 3: 结束

divsum(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
  • 步骤 6: 结束

Java 程序

输出

Enter the number? 23
The number is deficient.

C 语言程序

输出

Enter the number? 21
The number is deficient.

Python 程序

输出

Enter the number? 45
The number is deficient.

C# 程序

输出

Enter the number? 45
The number is deficient.

PHP 程序

输出

Enter the number? 45
The number is deficient.
下一主题#