-
Notifications
You must be signed in to change notification settings - Fork 0
/
amicable_numbers.m
45 lines (36 loc) · 1000 Bytes
/
amicable_numbers.m
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
% Find First 10 Amicable Numbers
% Amicable numbers are two distinct positive integers ...
% where each number is the sum of the proper divisors of the other.
% divisors of 6 are 1,2,3,6
% proper divisors of 6 are 1,2,3
% The sum of proper divisors of 6 is 6
%
clc;clear;
fprintf(" First 10 Amicable Numbers : \n\n")
count = 1;
for num= 2:100000
s = sumPDiv(num) ;
if s > num
s1 = sumPDiv(s) ;
if s1 == num
fprintf("%10d %10d\n",num,s)
count = count + 1 ;
if count > 10
break ;
end
end
end
end
function divisors = find_divisors(n) %find divisors of an integer
divisors=[];
for i=1:n
if mod(n,i)==0
divisors=[divisors,i];
end
end
divisors;
end
function sum_of_proper_divs = sumPDiv(n) %find sum of proper divisors of an integer
divs = find_divisors(n);
sum_of_proper_divs = sum(divs) - n;
end