# Is it a geometric sequence or not? [closed]

Well, last time I asked for an arithmetic sequence, now comes the geometric sequence

# Challenge

In this challenge, the input will be an unordered set of numbers and the program should be able to tell if the set of numbers is a Geometric Sequence.

# Input

• The input will be a set separated by ,(comma).

# Output

• The Output can be a Boolean (True or False), or a number (1 for True, 0 for False).

# Test cases

In: 1,3,9
Out: True

In: 1,4,16
Out: 1

In: 2,6,14
Out: 0

In: 2,8,4
Out: True

In: 4,6,9
Out: 1


# Rules

• This is , so the shortest code by bytes wins.
• The minimum length of the input set would be 3 elements.

Best of Luck

• Another test case: 4,6,9 – isaacg Jul 5 '20 at 17:42
• For your next challenge, I'd highly recommend posting in the Sandbox to get any feedback on your next idea, helping you avoid the issues you experienced with the arithmetic sequence challenge – caird coinheringaahing Jul 5 '20 at 17:47
• @Jonah Notice that the input is an unordered set. So 2, 4, 8 is a geometric sequence, and 2, 8, 4 is an unordered version of that set – caird coinheringaahing Jul 5 '20 at 18:47
• @Tanmay Both Wikipedia and the link you provided allow negative common ratio, so please add your definition to the post if you define geometric sequence differently. Also, are the input guaranteed to be positive integers? – Surculose Sputum Jul 5 '20 at 19:13
• Can the input contain duplicates, and if yes, what's the correct output for 1,1,1? Is 0,0,0 a valid input, and if yes, what's the correct output? – Zgarb Jul 5 '20 at 20:02

# J, 1514 13 bytes

1=&#&=2%/\/:~


Try it online!

-1 byte thanks to xash

-1 byte thanks to Bubbler

• 1= Does one equal...
• &#&= the length of the uniq of...
• 2%/\ the list created by dividing each element by its right neighbor in the input list...
• /:~ sorted.

# Jelly, 4 bytes

Ṣ÷ƝE


Try it online!

Thanks to fireflame241 for pointing out a mistake

## How it works

Ṣ÷ƝE - Main monadic link, takes an array as input
- e.g                            A = [2, 8, 4] or   A = [2, 6, 14]
Ṣ    - Sort                               [2, 4, 8]          [2, 6, 14]
Ɲ  - Over overlapping pairs [x, y]...   [[2, 4], [4, 8]]   [[2, 6], [6, 14]]
÷   - ...divide x by y?                  [0.5, 0.5]         [0.33, 0.43]
E - Is the list all the same?          1                  0

• Fails for 2,4,16. I'd suggest "quotient all equal" instead of "all divisible" – fireflame241 Jul 5 '20 at 18:03
• @fireflame241 I'd suggest that test case to be added to the challenge, as I'm sure it could catch out a lot of potential solutions – caird coinheringaahing Jul 5 '20 at 18:05
• @fireflame241 Corrected, using that tip, for no gains of bytes.Thanks! – caird coinheringaahing Jul 5 '20 at 18:08
• Isaacg's 4,6,9 already would cover this situation if OP adds it – fireflame241 Jul 5 '20 at 18:09
• 4,6,9 is a geometric sequence (r=1.5), so it should have been 1 – fireflame241 Jul 5 '20 at 18:11

# APL (Dyalog Unicode), 25 19 bytes

{~∨/⌈(⊣-⊃)2÷/⍵[⍋⍵]}


Try it online!

⍵[⍋⍵]      ⍝ sort ⍵
2÷/        ⍝ Take the quotient of all consecutive pairs
⍝ Check if all are equal:
(⊣-⊃)       ⍝ Subtract the first quotient
~∨/⌈        ⍝ Are all 0?

• ({÷/⍵}⌺2)2÷/ – Adám Jul 5 '20 at 21:58
• @Adám Good tip! Just curious here, is there an analog to ⌺ (on a higher-dimensional array, so not /) that only includes regions completely enclosed in the input, not just centered on an element in the input array? Best I could come up with is either using / and ⌿ on different axes or using ↑ on rotated views of the input array. – fireflame241 Jul 7 '20 at 16:01
• No, there isn't. You'll have to pad until it can complete a full window outside your actual data, and then chop that off. Try it online! – Adám Jul 7 '20 at 16:48

# Retina, 56 bytes

N\d+
Lv$\b\d+,(\d+),(\d+) ;$2**_;$1*$1*
A(;_+)\1\b
^$ Try it online! Link includes test suite. Explanation: N\d+  Sort in ascending order. Lv$\b\d+,(\d+),(\d+)


List all triples of numbers.

;$2**_;$1*$1*  For each triple a,b,c calculate ca and b². A(;_+)\1\b  Delete all lines where they equal. ^$


Check that there were no unequal triples.

Note that this runs in unary so it fails on large numbers, but you can use this similar 62-byte program that does the calculations in decimal instead:

N\d+
Lv$\b\d+,(\d+),(\d+) ;$.($2**);$.($1*$1*
A(;.+)\1\b
^\$


# JavaScript (ES6), 47 bytes

Returns a Boolean value.

a=>!a.sort((a,b)=>b-a).some(p=n=>a-(a=p/(p=n)))


Try it online!

# Python 3, 56 55 bytes

Saved a byte thanks to Surculose Sputum!!!

lambda l:l.sort()!=len({b/a for a,b in zip(l,l[1:])})<2


Try it online!

• You can do l.sort()<len(...)<2 in Python 2, but the input needs to be all floats for the division to work. Alternately in Python 3, l.sort()!=len(...)<2 – Surculose Sputum Jul 5 '20 at 21:08
• @SurculoseSputum Hmm, the Python 2 tip worked but I guess that was just luck that the integer divisions worked. Going with the Python 3 tip - thanks! :D – Noodle9 Jul 5 '20 at 21:17

# R, 38 bytes

function(x)!sd(diff(y<-sort(x))/y[-1])


Try it online!

• sort x

• calculate difference between successive elements

• divide by value of each element (except first) = fold-difference

• check if results are all the same (standard deviation = zero)

Fails for negative fold-differences between elements (which seems to be a true geometric sequence, but it isn't clear whether this is required by the challenge). Fixed by sorting x by absolute value, at a cost of +9 bytes:

47 bytes

function(x)!sd(diff(y<-x[order(abs(x))])/y[-1])

• diff(log())? Ah, except that might fail for negative numbers – Giuseppe Jul 5 '20 at 20:31
• That's a nice idea but (as you say) I can't seem to wrangle it to work with negatives. – Dominic van Essen Jul 5 '20 at 20:49

# APL(NARS), 14 chars, 28 bytes

{1=≢∪2÷/⍵[⍒⍵]}


it seems 2÷/1 3 9 do 1÷3 3÷9 and 2,/1 3 9 make couples

  h←{1=≢∪2÷/⍵[⍒⍵]}
⎕fmt {⍵,⊂,h⍵}¨(1 3 9)(1 4 16)(2 6 14)(2 8 4)(4 6 9)
┌5─────────────────────────────────────────────────────────────────┐
│┌4─────────┐ ┌4──────────┐ ┌4──────────┐ ┌4─────────┐ ┌4─────────┐│
││      ┌1─┐│ │       ┌1─┐│ │       ┌1─┐│ │      ┌1─┐│ │      ┌1─┐││
││1 3 9 │ 1││ │1 4 16 │ 1││ │2 6 14 │ 0││ │2 8 4 │ 1││ │4 6 9 │ 1│││
││~ ~ ~ └~─┘2 │~ ~ ~~ └~─┘2 │~ ~ ~~ └~─┘2 │~ ~ ~ └~─┘2 │~ ~ ~ └~─┘2│
│└∊─────────┘ └∊──────────┘ └∊──────────┘ └∊─────────┘ └∊─────────┘3
└∊─────────────────────────────────────────────────────────────────┘
`