# Am I a Cullen Number?

A Cullen Number is any number that is contained in the sequence generated using the formula:

C(n) = (n*2^n)+1.

Write a program or function that receives an input and outputs a truthy/falsy value based on whether the input is a Cullen Number.

## Input:

A non-negative integer between 0 and 10^9 (inclusive).

## Output:

A truthy/falsy value that indicates whether the input is a Cullen Number.

## Test Cases:

Input:    Output:
1   --->  truthy
3   --->  truthy
5   --->  falsy
9   --->  truthy
12  --->  falsy
25  --->  truthy

## Scoring:

This is , so the lowest score in bytes wins.

• What's the range of n? In particular, is 1 a Cullen Number?
– user62131
Jun 17 '17 at 20:15
• @ais523 according to OEIS, it is. n seems to be 0-based. Jun 17 '17 at 20:25
• Fair enough. Just needed to know whether my Jelly answer should have an or R in it :-)
– user62131
Jun 17 '17 at 20:25
• Related Jun 17 '17 at 20:36
• Umm, what's with the downvote? Jun 20 '17 at 13:47

# Pyth, 6 5 bytes

/mh.<

try it online

## x86_64 machine code (System V ABI), 28 27 bytes

-1 byte thanks to @Cody Gray, thanks!

A constant-time algorithm!

_cullen:
0:   0f bd cf    bsrl    %edi, %ecx
3:   0f bd c1    bsrl    %ecx, %eax
6:   89 ca       movl    %ecx, %edx
8:   29 c2       subl    %eax, %edx
a:   0f bd c2    bsrl    %edx, %eax
d:   29 c1       subl    %eax, %ecx
f:   d3 e1       shll    %cl, %ecx
11:   ff c1       incl    %ecx
13:   31 c0       xorl    %eax, %eax
15:   39 f9       cmpl    %edi, %ecx
17:   0f 94 c0    sete    %al
1a:   c3          retq

Explanation:

Let y an integer and x=y*2^y + 1. Taking logs, we have y + log2(y) = log2(x-1), thus y=log2(x-1)-log2(y). Plugging back the value of y, we get y=log2(x-1)-log2(log2(x-1)-log2(y)). Doing this one more time, we obtain: y=log2(x-1)-log2[log2(x-1)-log2(log2(x-1)-log2(log2(x-1)-log2(y)))].

Let us remove the last terms (of the order of log2(log2(log2(log2(x)))), this should be safe!), and assume that x-1≈x, we obtain: y≈log2(x)-log2[log2(x)-log2(log2(x))]

Now, letting f(n) = floor(log2(n)), it can be verified manually that y can be exactly retrieved by: y=f(x)-f[f(x)-f(f(x))], for y < 26, and thus x ⩽ 10^9, as specified by the challenge(1).

The algorithm then simply consists of computing y given x, and verify that x == y*2^y + 1. The trick is that f(n) can simply be implemented as the bsr instruction (bit-scan reverse), which returns the index of the first 1-bit in n, and y*2^y as y << y.

Detailed code:

_cullen:                                 ; int cullen(int x) {
0:   0f bd cf    bsrl    %edi, %ecx   ;  int fx = f(x);
3:   0f bd c1    bsrl    %ecx, %eax   ;  int ffx = f(f(x));
6:   89 ca       movl    %ecx, %edx
8:   29 c2       subl    %eax, %edx   ;  int a = fx - ffx;
a:   0f bd c2    bsrl    %edx, %eax   ;  int ffxffx = f(a);
d:   29 c1       subl    %eax, %ecx   ;  int y = fx - ffxffx;
f:   d3 e1       shll    %cl, %ecx    ;  int x_ = y<<y;
11:   ff c1       incl    %ecx         ;  x_++;
13:   31 c0       xorl    %eax, %eax
15:   39 f9       cmpl    %edi, %ecx
17:   0f 94 c0    sete    %al
1a:   c3          retq                 ;  return (x_ == x);
; }

(1)In fact, this equality seems to hold for values of y up to 50000.

• Well, I'm pretty sure this qualifies as the most interesting code for this challenge so far. +1 Jun 18 '17 at 14:30
• Pre-XORing eax would allow you to eliminate the movzbl, saving 1 byte. You'd need to do the XOR before the cmpl so it doesn't clobber the flags, of course, but that's totally fine because nothing after that depends on eax. Or, you could just decide that the method returns a Boolean in only the lower 8 bits, saving all 3 bytes! Jun 19 '17 at 11:41
• @CodyGray Indeed, thanks a lot :) Jun 19 '17 at 14:46

# Jelly, 7 6 bytes

Ḷæ«i’

Try it online!

Takes input as a command-line argument. If given a Cullen number C(n), outputs n+1 (which is truthy in Jelly, being an nonzero integer; note that we have n≥0 because the input is an integer, and Cullen numbers with negative n are never integers). If given a non-Cullen number, returns 0, which is falsey in Jelly.

## Explanation

Ḷæ«i’
Ḷ        Form a range from 0 to (the input minus 1)
æ«      Left-shift each element in the range by
       itself
i’   Look for (the input minus 1) in the resulting array

Basically, form an array of Cullen numbers minus one, then look for the input minus one in it. If the input is a Cullen number, we'll find it, otherwise we won't. Note that the array is necessarily long enough to reach to the input, because C(n) is always greater than n.

## JavaScript (ES6), 37 35 bytes

Saved 2 bytes thanks to Neil

f=(n,k,x=k<<k^1)=>x<n?f(n,-~k):x==n

### Demo

f=(n,k,x=k<<k^1)=>x<n?f(n,-~k):x==n

console.log(JSON.stringify([...Array(1000).keys()].filter(n => f(n))))

• Does x<n?f(n,k+1):x==n work?
– Neil
Jun 17 '17 at 20:57
• @Neil It sure does. :-) Jun 17 '17 at 21:13
• Why does ~k work, while k+1 overloads the callstack? Jun 18 '17 at 20:17

f n=or[x*2^x+1==n|x<-[0..n]]

Try it online!

# Ohm, 8 bytes

@Dº*≥Dlε

Try it online!

Implicit input
@          Range [1,...,Input]
D         Duplicate
º        2^n each element
*       Multiply those two array
≥      Increment everything (now I have an array of all Cullen Numbers)
Dl    Push array length (= get input again, can't get again implicitly or using a function because it would be a string so I'd waste a byte again)
ε   Is input in array?

# PHP, 43 bytes

for(;$argn>$c=1+2**$n*$n++;);echo$argn==$c;

Try it online!

• Is $argn a special variable? Changing it to$a would save 6 bytes: tio.run/##K8go@G9jX5BRwKWSaKtkaGaoZP0/… Jun 19 '17 at 15:39

Try it online!

## Explanation

$0 n is 0-indexed ? Mode query. Given input n, output true/false for if n is in the sequence. 1+$2^\$    Each item in the sequence equals 1+index*2^index

# TI-BASIC, 17 bytes

max(Ans=seq(X2^X+1,X,0,25

Explanation

seq(X2^X+1,X,0,25 Generate a list of Cullen numbers in the range
Ans=              Compare the input to each element in the list, returning a list of 0 or 1
max(              Take the maximum of the list, which is 1 if any element matched
• You might want to add an explanation to this. Aug 1 '17 at 23:58
• Done, thanks for the tip. Aug 2 '17 at 0:12
• That works, but a command-by-command explanation usually helps garner most upvotes. I would reccomend doing something like the explanation on this answer. I don't know why somebody downvoted your post though. It's usually common courtesy to leave a comment when you do so, although that idea is often ignored. Aug 2 '17 at 0:24
• Your welcome. I remember when I first joined the site, people told these types of things to me. Just passing on the favour. Aug 2 '17 at 0:32

# QBIC, 24 bytes

[0,:|~a*(2^a)+1=b|_Xq}?0

## Explanation

[0,:|           FOR a = 0 to b (input from cmd line)
~a*(2^a)+1=b    IF calculating this a results in b
|_Xq            THEN quit, printing 1
}               NEXT a
?0              We haven't quit early, so print 0 and end.

# k, 19 bytes

{&1=x-{x**/x#2}'!x}

Try it online. Truthy is an array with a number in it: ,3 or ,0 et cetera. Falsey is an empty array: () or !0 depending on your interpreter.

# Java (OpenJDK 8), 56 bytes

i->{int n,c;for(n=0;(c=n*(2<<n++-1)+1)<i;);return c==i;}

Try it online!

# Pari/GP, 25 bytes

n->!prod(i=0,n,n-i*2^i-1)

Try it online!