LoginSignup
7
5

More than 5 years have passed since last update.

約数の和を求める方法

Last updated at Posted at 2019-04-01

約数の和を求める方法

約数とは、ある整数や整式に対してそれを割り切ることのできる整数や整式のことです。
10なら、1、2、5、10となります。
約数の和を求める方法を以下に記述します。

$num = 10; //与える数字
$a = 0;

// $i…与えられた数字を割る値
for($i=1; $i <= $num; $i++){
    // 余りが0の場合、$iは$numの約数となります
    if($num%$i==0){
        $a += $i;
    }
}
print $a;

実行結果:18

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5