-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalldivisors.m
More file actions
25 lines (19 loc) · 723 Bytes
/
Copy pathalldivisors.m
File metadata and controls
25 lines (19 loc) · 723 Bytes
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
%"Copyright 2023 - 2024 The MathWorks, Inc."
function divs = alldivisors(N)
% compute the set of all integer divisors of the positive integer N
% first, get the list of prime factors of N.
facs = factor(N);
divs = [1,facs(1)];
for fi = facs(2:end)
% if N is prime, then facs had only one element,
% and this loop will not execute at all, In that case
% The set of all divisors is simply 1 and N.
% this outer product will generate all combinations of
% the divisors found so far, combined with the current
% divisor fi.
divs = [1;fi]*divs;
% unique eliminates the replicate divisors, making
% this an efficient code.
divs = unique(divs(:)');
end
end