Number of four-term GPs consisting of positive integers under 100: how to write a program

52 Views Asked by At

Note: This is a past question from Brilliant.org

I was recently thinking about how to write a program which fetches you the number of 4-term geometric progressions consisting of positive integers under 100. I solved this problem on a website called Brilliant.org by considering the common ratios and the first terms, and got the right answer (which I don't want to disclose because they do use past questions again and again).

I want to learn how to write a program which solves this question, which I'd again write for clarity:

What is the number of 4-term geometric progressions consisting entirely of positive integers less than or equal to 100?

Any language, preferably Sage, would work for me. I'd appreciate it if you write a pseudocode.

1

There are 1 best solutions below

2
On BEST ANSWER

Bruteforce approach works well for numbers $\leq 100$. Try the following Haskell one-liner:

length [ [a,b,c,d] | a <- [1..100], b <- [1..100], c <- [1..100], d <- [1..100], a*c == b*b, b*d == c*c]

or this one in python (which is the base language of Sage):

len([ (a,b,c,d) for a in range(1,101) for b in range(1,101) for c in range(1,101) for d in range(1,101) if a*c == b*b and b*d == c*c])

I hope this helps ;-)