代码之家  ›  专栏  ›  技术社区  ›  Rebel

如何以最有效的方式加速和并行化以下matlab代码?

  •  0
  • Rebel  · 技术社区  · 1 年前

    使用 matlab ,我试图找到一组六个整数, x、 y、z、a、b、c 这将满足以下方程式:

    x^a+y^b=z^c

    min_num = 1; max_num = 100;
    [x, y, z, a, b, c] = deal(zeros(min_num, max_num));
    
    n=1;
    for x_ = min_num:max_num
        for y_ = min_num:max_num
            for z_ = min_num:max_num
                for a_ = min_num:max_num
                    for b_ = min_num:max_num
                        for c_ = min_num:max_num
                            if x_^a_ + y_^b_ == z_^c_
                                x(n)=x_; y(n)=y_; z(n)=z_; a(n)=a_; b(n)=b_; c(n)=c_;
                                n=n+1;
                            end
                        end
                    end
                end
            end
        end
    end
    Pyth=[x',y',z', a',b',c'];
    disp(Pyth)
    

    这是我第一次尝试做这项工作:

    min_num = 1; max_num = 10;
    
    
    % simSpace is the size of the space you're exploring
    simSpace = [max_num, max_num, max_num, max_num, max_num, max_num];
    % calculate the total number of simulations
    numSims  = prod(simSpace);
    
    % pre-allocate data
    data = zeros(numSims, max_num+1);
    
    parfor idx = 1:numSims
        % convert scalar index into subscripts
        [i, j, k, l, m, n] = ind2sub(simSpace, idx);
        if i^l + j^m == k^n
            disp([i, j, k, l, m, n])
        end
    end
    

    然而,我想知道我是否能做得更好。

    0 回复  |  直到 1 年前