I can have any number row which consists from 2 to 10 numbers. And from this row, I have to get geometrical progression.
For example:
Given number row: 125 5 625 I have to get answer 5. Row: 128 8 512 I have to get answer 4.
Can you give me a hand? I don’t ask for a program, just a hint, I want to understand it by myself and write a code by myself, but damn, I have been thinking the whole day and couldn’t figure this out.
Thank you.
DON’T WRITE THE WHOLE PROGRAM!
Guys, you don’t get it, I can’t just simple make a division. I actually have to get geometrical progression + show all numbers. In 128 8 512 row all numbers would be: 8 32 128 512
Seth’s answer is the right one. I’m leaving this answer here to help elaborate on why the answer to
128 8 512is4because people seem to be having trouble with that.A geometric progression’s elements can be written in the form
c*b^nwherebis the number you’re looking for (bis also necessarily greater than 1),cis a constant andnis some arbritrary number.So the best bet is to start with the smallest number, factorize it and look at all possible solutions to writing it in the
c*b^nform, then using thatbon the remaining numbers. Return the largest result that works.So for your examples:
Start with 5. 5 is prime, so it can be written in only one way:
5 = 1*5^1. So yourbis 5. You can stop now, assuming you know the row is in fact geometric. If you need to determine whether it’s geometric then test thatbon the remaining numbers.8can be written in more than one way:8 = 1*8^1,8 = 2*2^2,8 = 2*4^1,8 = 4*2^1. So you have three possible values forb, with a few different options forc. Try the biggest first.8doesn’t work. Try4. It works!128 = 2*4^3and512 = 2*4^4. Sobis4andcis2.This one is a bit mean because the first number is prime but isn’t
b, it’sc. So you’ll need to make sure that if your firstb-candidate doesn’t work on the remaining numbers you have to look at the next smallest number and decompose it. So here you’d decompose 15:15 = 15*?^0(degenerate case),15 = 3*5^1,15 = 5*3^1,15 = 1*15^1. The answer is 5, and3 = 3*5^0, so it works out.