5
\$\begingroup\$

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

\$\endgroup\$
12
  • 2
    \$\begingroup\$ Another test case: 4,6,9 \$\endgroup\$ – isaacg Jul 5 '20 at 17:42
  • 5
    \$\begingroup\$ 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 \$\endgroup\$ – caird coinheringaahing Jul 5 '20 at 17:47
  • 2
    \$\begingroup\$ @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 \$\endgroup\$ – caird coinheringaahing Jul 5 '20 at 18:47
  • 5
    \$\begingroup\$ @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? \$\endgroup\$ – Surculose Sputum Jul 5 '20 at 19:13
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Zgarb Jul 5 '20 at 20:02
4
\$\begingroup\$

J, 15 14 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.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 14 bytes: 1=&#&~.2%/\/:~ \$\endgroup\$ – xash Jul 6 '20 at 1:42
  • 1
    \$\begingroup\$ 13 bytes by using monadic = in place of ~. \$\endgroup\$ – Bubbler Jul 6 '20 at 2:11
7
\$\begingroup\$

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
\$\endgroup\$
8
  • \$\begingroup\$ Fails for 2,4,16. I'd suggest "quotient all equal" instead of "all divisible" \$\endgroup\$ – fireflame241 Jul 5 '20 at 18:03
  • \$\begingroup\$ @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 \$\endgroup\$ – caird coinheringaahing Jul 5 '20 at 18:05
  • \$\begingroup\$ @fireflame241 Corrected, using that tip, for no gains of bytes.Thanks! \$\endgroup\$ – caird coinheringaahing Jul 5 '20 at 18:08
  • \$\begingroup\$ Isaacg's 4,6,9 already would cover this situation if OP adds it \$\endgroup\$ – fireflame241 Jul 5 '20 at 18:09
  • 1
    \$\begingroup\$ 4,6,9 is a geometric sequence (r=1.5), so it should have been 1 \$\endgroup\$ – fireflame241 Jul 5 '20 at 18:11
2
\$\begingroup\$

APL (Dyalog Unicode), 25 19 bytes

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

Try it online!

-6 bytes thanks to @Adám

⍵[⍋⍵]      ⍝ sort ⍵
2÷/        ⍝ Take the quotient of all consecutive pairs
           ⍝ Check if all are equal:
(⊣-⊃)       ⍝ Subtract the first quotient
~∨/⌈        ⍝ Are all 0?
\$\endgroup\$
3
  • 1
    \$\begingroup\$ ({÷/⍵}⌺2)2÷/ \$\endgroup\$ – Adám Jul 5 '20 at 21:58
  • \$\begingroup\$ @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. \$\endgroup\$ – fireflame241 Jul 7 '20 at 16:01
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Adám Jul 7 '20 at 16:48
1
\$\begingroup\$

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 .

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
^$
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$ – Surculose Sputum Jul 5 '20 at 21:08
  • \$\begingroup\$ @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 \$\endgroup\$ – Noodle9 Jul 5 '20 at 21:17
1
\$\begingroup\$

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])
\$\endgroup\$
2
  • \$\begingroup\$ diff(log())? Ah, except that might fail for negative numbers \$\endgroup\$ – Giuseppe Jul 5 '20 at 20:31
  • \$\begingroup\$ That's a nice idea but (as you say) I can't seem to wrangle it to work with negatives. \$\endgroup\$ – Dominic van Essen Jul 5 '20 at 20:49
1
\$\begingroup\$

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
└∊─────────────────────────────────────────────────────────────────┘
\$\endgroup\$

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