Evaluated Powers of Bases if gcd of 3 bases is 1 without using tons of for loops

Viewed 96

I'm trying to make a program that will evaluate the gcd of 3 numbers (the bases), and if the GCD of base1 and base2, base2 and base3, and base3 and base1 are all equal to one, then evaulate the bases to exponents in a range. Basically, what I need to do is figure out if their GCD's are equal to one, then calculate the numbers to powers. Here's what it would look like:

bases = 150
powers = 150
base1 = all numbers 1-bases
base2 = all numbers 1-bases
base3 = all numbers 1-bases
if the GCD of all combinations = 1
    do base1^all numbers 3-powers
    do base2^all numbers 3-powers
    do base2^all numbers 3-powers
    then store all of those in an array

Now, I've tried using the dreaded for loops, but it is very slow and I'm not considering it a solution. It only works quickly if bases and powers are 10 or below. How can I do this without using for loops? Or, if I have to use a for loop, how can I reduce the number that is used? Could I computate what numbers together's 3 GCD combinations equal 1? The for loop that I've tried is below:

for i = 1:numbers
    for j = 1:numbers
        for k = 1:numbers
            if gcd(i,j) == 1 && gcd(i,k) == 1 && gcd(k,j) == 1
                for a = 3:powers
                    for b = 3:powers
                        for c = 3: powers
                            x = i^a
                            y = j^b
                            z = k^c
                        end
                    end
                end
            end
        end
    end
end
2 Answers
Related