# Is it a doubling sequence?

A doubling sequence is an array of numbers where each subsequent number is at least twice the previous number.

Given an ordered list of numbers, determine if the numbers in the list ($$\n_{x}\$$) have the property that:

$$\n_1 \times 2 \le n_2\$$

$$\n_2 \times 2 \le n_3\ ...\$$

and so on until reaching the end of the list.

# Input

A list of two or more numbers.

# Output

Any distinct True or False value.

# Examples:

[10,20,30] -> False
[10,20,40] -> True
[1,2,3] -> False
[1,2,4] -> True
[1,2,10] -> True
[1,1] -> False
[10,1] -> False

• Can input be sorted in either order? Feb 12, 2020 at 18:20
• Pre-sorted in that it may be sorted ascending, descending, or some other order, but you don't have to change the order of the list that is fed to the function. Feb 12, 2020 at 18:20
• Since inputs can be non-integers, do we care about floating point precision? Many of these solution fail for n_i+1 = n_i*(2-epislon) for some fairly large epsilons
– Vlo
Feb 12, 2020 at 19:40
• what about negative numbers? what about the sequence of zeros? Feb 12, 2020 at 20:03
• "some other order" can be any reflexive transitive antisymmetric relation. In other words, your definition admits arbitrary order of the elements, which I'd guess is not what you intended. Feb 12, 2020 at 22:19

# Python 3, 52 bytes

lambda l:all(2*l[i]<=l[i+1]for i in range(len(l)-1))


Try it online!

# APL (Dyalog Extended), 18 bytes

∧/{(2×¯1↓⍵)≤(1↓⍵)}


Try it online!

Decomposition/Explanation:

⍵ is the input vector (test cases). Output will be 1 or 0; 1 is TRUE/truthy, 0 is FALSE/falsey.

(1↓⍵)  - Drop the first element from the input vector (call it LASTN)
¯1↓⍵         - Drop the last element from the input vector, separately, and
(2×    )        - multiply it by two (call the result DFIRSTN)
≤       - compare corresponding elements - Each element of LASTN should be
equal to or greater than the corresponding element of DFIRSTN.
{              } - encapsulate as a dfn to apply to the input vector. This dfn will
return 0 for elements where the comparison is FALSE, 1 where TRUE
∧/                 - AND (Boolean conjunction) reduction of the vector. ∧/ A B C D ... is
equivalent to A ∧ B ∧ C ∧ D ∧ ..., and if there are any FALSE values
in the vector, the result will be FALSE (0)


The linked TIO has the test cases and a REPL; you can test your own vectors by changing the input field, one vector per line.

# C (gcc), 51 bytes

r;f(a,l)int*a;{for(r=--l;l--;)r=*a*2>*++a?0:r;a=r;}


Pretty simple. For every element of the array, check if the current element times two is greater than the next element.

Returns zero if it is not a doubling sequence and non-zero (or more specifically the length minus one) otherwise.

-10 bytes thanks to ceilingcat!

Try it online!

• @ceilingcat Thanks. I was trying to find a solution with no counter variable. Feb 14, 2020 at 20:28
• 49 bytes Feb 14, 2020 at 21:07

# T-SQL, 64 bytes

This can handle positive values above 0

Returns -1 for true, 0 for false

SELECT~min(1/~x)FROM(SELECT a/lag(2*a)over(ORDER BY i)x FROM @)y


Try it online

# C++ (gcc), 757167 65 bytes

int f(int*a,int s){int r=1;for(;--s;)r*=a[s]>=2*a[s-1];return r;}


Try it online!

• @ceilingcat Thanks for the input! I thought the i++ placement was UB, so I dumped the variable - 67 bytes! Feb 23, 2020 at 15:24
• @ceilingcat Should've thought of that! Mar 1, 2020 at 18:01
• 64 Dec 7, 2020 at 10:06

# Rockstar, 101 100 bytes

listen to F
D's0
while F
listen to T
let R be F/T-.5
turn up R
let D be D or R
let F be T

say not D


Try it here (Code will need to be pasted in, with each input integer on an individual line)

# Wolfram Language (Mathematica), 25 15 bytes

Min@Ratios@#<2&


Returns True for false and False for true.

-10 bytes thanks to @att

Try it online!

• 15 bytes
– att
Sep 15, 2020 at 21:24