Groovy web console

subscribe to the feed Subscribe
to this
site
Find all factor pairs of a number (revised) (via #groovywebconsole)
tweet this snippet Tweet
this
script

Find all factor pairs of a number (revised)

Published 2 months ago by S.J.M with tags Factors Math
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
//  Find all factor pairs of a number
//  SJM

def listOfFactors(Integer n) {
    List a = []

    for (i in 1..<Math.sqrt(n)+1 ) {
        if (n % i == 0) { a << [i, n/i] }
    }
    return a
}

//change n as desired:
def n = 1200

def factorsOfN = listOfFactors(n)

for (j in 0..<factorsOfN.size()) { println(factorsOfN[j]) }