Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Note: This is a past question from

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 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.

share|cite|improve this question
up vote 1 down vote accepted

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 ;-)

share|cite|improve this answer
Your code makes sense, but here's just something you missed: you can reverse your list and that would still be a valid geometric progression. – Parth Kohli Mar 30 '13 at 11:41
@pen Yeah, I have already fixed it. – dtldarek Mar 30 '13 at 11:41
Thanks! :-)${}$ – Parth Kohli Mar 30 '13 at 11:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.