AliquotSum [真因数和]
# 介绍
在数论中,整数的真因数和又称真因子和是指该整数的所有真因数之和,即除了自己本身外的所有正因数之和,通常以 来表示:。真因数和可以用来描述质数、完全数、亏数、过剩数和不可及数,也可以用于定义整数的真因数和数列。
1 是唯一一个真因数和为 0 的正整数。如果一个正整数真因数和为 1 则代表该数是一个质数。
# 实现
# JavaScript
/*
A program to calculate the Aliquot Sum of a number.
The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself
For example, for the number 6
The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6
1 is the only number whose aliquot sum is 0 (since its only divisor is 1 and aliquot sum of a number couldn't have itself)
For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1
Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum
*/
/**
* @param {Number} input The number whose aliquot sum you want to calculate
*/
function aliquotSum (input) {
// input can't be negative
if (input < 0) throw new TypeError('Input cannot be Negative')
// input can't be a decimal
if (Math.floor(input) !== input) throw new TypeError('Input cannot be a Decimal')
// Dealing with 1, which isn't a prime
if (input === 1) return 0
let sum = 0
for (let i = 1; i <= (input / 2); i++) {
if (input % i === 0) sum += i
}
return sum
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 参考
编辑 (opens new window)
上次更新: 2022/10/18, 20:15:06