如何计算八度音程中所有因子的因子(不仅仅是素数因子)?

时间:2011-10-15 18:33:36

标签: octave

我是Octave的初级用户,想要计算一个数字的所有整数除数,例如,对于数字120,我想得到1,2,3,4,5,6,8,10 ,12,15,20,24,30,40,60和120。

我知道八度音程具有factor功能,但这只给出了素数分解。我想要所有整数除数。

3 个答案:

答案 0 :(得分:1)

我不认为这是一个内置功能,所以你需要写一个。由于每个因子都是素因子集的乘积,因此您可以使用一些内置函数来构建所需的结果。

function rslt = allfactors(N)
%# Return all the integer divisors of the input N
%# If N = 0, return 0
%# If N < 0, return the integer devisors of -N
    if N == 0
        rslt = N;
        return
    end
    if N < 0
        N = -N;
    end

    x = factor(N)'; %# get all the prime factors, turn them into a column vector  '
    rslt = []; %# create an empty vector to hold the result
    for k = 2:(length(x)-1)
        rslt = [rslt ; unique(prod(nchoosek(x,k),2))];
        %# nchoosek(x,k) returns each combination of k prime factors
        %# prod(..., 2) calculates the product of each row
        %# unique(...) pulls out the unique members
        %# rslt = [rslt ...] is a convenient shorthand for appending elements to a vector
    end
    rslt = sort([1 ; unique(x) ; rslt ; N]) %# add in the trivial and prime factors, sort the list
end

答案 1 :(得分:1)

我觉得这是你要找的那个。 希望它可能有用。

MATLAB / Octave

function fact(n);
f = factor(n);  % prime decomposition    
K = dec2bin(0:2^length(f)-1)-'0';   % generate all possible permutations    
F = ones(1,2^length(f));        
for k = 1:size(K)      
   F(k) = prod(f(~K(k,:)));         % and compute products    
end;     
F = unique(F);                      % eliminate duplicates    
printf('There are %i factors for %i.\n',length(F),n);    
disp(F);  
end;' 

以下是输出:

>> fact(12)
There are 6 factors for 12.
    1    2    3    4    6   12
>> fact(28)
There are 6 factors for 28.
    1    2    4    7   14   28
>> fact(64)
There are 7 factors for 64.
    1    2    4    8   16   32   64
>>fact(53)
There are 2 factors for 53.
    1   53

答案 2 :(得分:0)

只需使用“除数”:

divisors(sym(120))

ans = (sym) [1  2  3  4  5  6  8  10  12  15  20  24  30  40  60  120]  (1×16 matrix)