Am I a Cullen Number?

25

0

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

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

Your Task:

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.

Gryphon

Posted 2017-06-17T20:10:02.633

Reputation: 6 697

1What's the range of n? In particular, is 1 a Cullen Number? – None – 2017-06-17T20:15:58.903

3

@ais523 according to OEIS, it is. n seems to be 0-based.

– steenbergh – 2017-06-17T20:25:16.287

Fair enough. Just needed to know whether my Jelly answer should have an or R in it :-) – None – 2017-06-17T20:25:55.447

2Related – James – 2017-06-17T20:36:53.860

Umm, what's with the downvote? – Gryphon – 2017-06-20T13:47:15.620

Answers

7

Pyth, 6 5 bytes

/mh.<

try it online

jacoblaw

Posted 2017-06-17T20:10:02.633

Reputation: 264

16

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.

yoann

Posted 2017-06-17T20:10:02.633

Reputation: 640

4Well, I'm pretty sure this qualifies as the most interesting code for this challenge so far. +1 – Gryphon – 2017-06-18T14:30:31.580

1Pre-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! – Cody Gray – 2017-06-19T11:41:59.913

@CodyGray Indeed, thanks a lot :) – yoann – 2017-06-19T14:46:36.103

7

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.

user62131

Posted 2017-06-17T20:10:02.633

Reputation:

7

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))))

Arnauld

Posted 2017-06-17T20:10:02.633

Reputation: 111 334

Does x<n?f(n,k+1):x==n work? – Neil – 2017-06-17T20:57:40.770

@Neil It sure does. :-) – Arnauld – 2017-06-17T21:13:32.880

Why does `~k work, while k+1 overloads the callstack? – trlkly – 2017-06-18T20:17:14.770

@trlkly Basically, undefined+1===NaN but -~undefined===1. You can read more about this here.

– Arnauld – 2017-06-18T21:13:25.133

5

Haskell, 28 bytes

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

Try it online!

nimi

Posted 2017-06-17T20:10:02.633

Reputation: 34 639

3

R, 53 51 46 bytes

pryr::f(x%in%lapply(0:x,function(y)(y*2^y+1)))

Anonymous function. Checks if x is generated in the sequence C(n) for n in [0,x].

3 bytes golfed by Giuseppe.

Try it online!

BLT

Posted 2017-06-17T20:10:02.633

Reputation: 931

use x%in%... instead of any(x==...); that'll drop you 4 bytes – Giuseppe – 2017-06-17T21:56:52.880

So if I golf this by changing the lapply to simply checking the vector, and use scan instead of taking function arguments - I get @giuseppe 's answer. Thanks for posting it separately so I can see what I'm missing - I learn more by trying something on my own, even though I usually lose. – BLT – 2017-06-18T01:09:25.323

3

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?

FrodCube

Posted 2017-06-17T20:10:02.633

Reputation: 539

3

PHP, 43 bytes

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

Try it online!

Jörg Hülsermann

Posted 2017-06-17T20:10:02.633

Reputation: 13 026

Is $argn a special variable? Changing it to $a would save 6 bytes: https://tio.run/##K8go@G9jX5BRwKWSaKtkaGaoZP0/Lb9Iw1ol0U4l2dZQ20hLSyUPiLS1rTWtU5Mz8oHqbFWSrf//BwA

– topher – 2017-06-19T15:39:39.137

@topher Yes $argn is available if you run PHP from the command line with the -R option – Jörg Hülsermann – 2017-06-19T16:26:36.873

3

05AB1E, 7 bytes

ÝDo*¹<å

Try it online!

Explanation:

ÝDo*¹<å Example input: 9. Stack: [9]
Ý       Range 0-input. Stack: [[0,1,2,3,4,5,6,7,8,9]]
 D      Duplicate. Stack: [[0,1,2,3,4,5,6,7,8,9],[0,1,2,3,4,5,6,7,8,9]]
  o     2** each item in the list. Stack: [[0,1,2,3,4,5,6,7,8,9], [1,2,4,8,16,32,64,128,256,512]]
   *    Multiply the two lists. Stack: [[0, 2, 8, 24, 64, 160, 384, 896, 2048, 4608]]
    ¹   Push input again. Stack: [[0, 2, 8, 24, 64, 160, 384, 896, 2048, 4608],9]
     <  Decrement. Stack: [[0, 2, 8, 24, 64, 160, 384, 896, 2048, 4608],8]
      å Is the first item of the stack in the second item? Stack: [1]
        Implicit print.

Comrade SparklePony

Posted 2017-06-17T20:10:02.633

Reputation: 5 784

3

C, C++, Java, C#, D : 70 bytes

Due to the similarities between all these languages, this code works for each

int c(int n){for(int i=0;i<30;++i)if((1<<i)*i+1==n)return 1;return 0;}

HatsuPointerKun

Posted 2017-06-17T20:10:02.633

Reputation: 1 891

I'll post the optimized D version this time, some pretty D-specific tricks can be used. – Zacharý – 2017-08-03T21:55:41.273

Suggest i=30;i--;)if(i<<i==n-1) instead of i=0;i<30;++i)if((1<<i)*i+1==n) – ceilingcat – 2018-11-28T04:24:02.600

2

Python 2, 36 bytes

f=lambda n,i=0:i<<i!=n-1and f(n,i+1)

Try it online!

Outputs by not crashing / crashing, as currently allowed by this meta concensus.


Python 2, 42 bytes

i=0
n=input()-1
while i<<i<n:i+=1
i<<i>n<c

Try it online!

Outputs via exit code

ovs

Posted 2017-06-17T20:10:02.633

Reputation: 21 408

2

Python, 40 bytes

lambda n:any(x<<x==n-1for x in range(n))

Try it online!

Uriel

Posted 2017-06-17T20:10:02.633

Reputation: 11 708

2

R, 26 bytes

a=0:26;scan()%in%(1+a*2^a)

Try it online!

A slightly different approach than the other R answer; reads from stdin and since the input is guaranteed to be between 0 and 10^9, we just have to check n between 0 and 26.

Giuseppe

Posted 2017-06-17T20:10:02.633

Reputation: 21 077

I never remember scan(). Good work. – BLT – 2017-06-17T22:10:17.740

2

APL (Dyalog), 9 bytes

To cover the case of n = 1, it requires ⎕IO←0 which is default on many systems.

⊢∊1+⍳×2*⍳

Try it online!

 [is] n (the argument)

 a member of

1 one

+ plus

 the integers 0 … (n-1)

× times

2 two

* to the power of

 the integers 0 … (n-1)

Adám

Posted 2017-06-17T20:10:02.633

Reputation: 37 779

So, "default on many systems" means that it just exists? – Zacharý – 2017-08-03T22:03:14.753

@Zacharý Yes, it would be wrong to call ⎕IO←0 non-standard, as many indeed always have it set like that, with no specification necessary each time. – Adám – 2017-08-04T04:10:03.070

Well. I'm going to use that trick in MY for sure (and MY can have non-0 and non-1 Index Origins) if I ever get the chance to. – Zacharý – 2017-08-04T20:37:10.907

@Zacharý Wouldn't that require an actual install base/versions where those values are default? E.g. in SAX and ngn, ⎕IO←0. – Adám – 2017-08-05T22:04:33.057

Yeah, I guess it would. And MY does have three iotas, so I think that wouldn't be used anyways. – Zacharý – 2017-08-05T22:12:15.180

2

Python 2, 32 bytes

[n<<n|1for n in range(26)].count

Try it online!

Creates the list of Cullen numbers up to 10^9, then counts how many times the input appears in it. Thanks to Vincent for pointing out n<<n|1 instead of (n<<n)+1, saving 2 bytes.

xnor

Posted 2017-06-17T20:10:02.633

Reputation: 115 687

You can save two bytes using n<<n|1 (n<<n being even) ;) – Vincent – 2017-06-18T09:41:19.280

This fails for 838860801. You need range(26), because the range isn't inclusive. – mbomb007 – 2017-06-19T16:52:17.867

@mbomb007 Thanks. I've done this for a while and still sometimes forget ranges are right-exclusive. – xnor – 2017-06-19T16:55:55.617

2

D, 65 bytes

This is a port of @HatsuPointerKun's algorithm to D (the original was already D code, but this is with D-specific tricks)

T c(T)(T n){for(T i;i<30;++i)if((1<<i)*i+1==n)return 1;return 0;}

How? (D specific tricks)

D's template system is shorter than C++'s, and can infer types. And D also initializes its variables to the default upon declaration.

Zacharý

Posted 2017-06-17T20:10:02.633

Reputation: 5 710

1

Mathematica, 30 bytes

MemberQ[(r=Range@#-1)2^r+1,#]&

Pure function taking a nonnegative integer as input and returning True or False. If the input is n, then (r=Range@#-1) sets the variable r to be {0, 1, ..., n-1}, and then r2^r+1 vectorially computes the first n Cullen numbers. MemberQ[...,#] then checks whether n is an element of the list.

Greg Martin

Posted 2017-06-17T20:10:02.633

Reputation: 13 940

1

Mathematica, 32 bytes

!Table[x*2^x+1,{x,0,#}]~FreeQ~#&

J42161217

Posted 2017-06-17T20:10:02.633

Reputation: 15 931

1

Excel VBA, 45 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] and ouputs to the VBE immediate window

Must be run in a clean module or have values for i,j be reset to default value of 0 between runs

While j<[A1]:j=(i*2 ^ i)+1:i=i+1:Wend:?j=[A1]

Input / Output

I/O as seen in VBE immediate window

[A1]=25
While j<[A1]:j=(i*2 ^ i)+1:i=i+1:Wend:?j=[A1]
True

[A1]=1: i=0:j=0 ''# clearing module values
While j<[A1]:j=(i*2 ^ i)+1:i=i+1:Wend:?j=[A1]
True    

[A1]=5: i=0:j=0 ''# clearing module values
While j<[A1]:j=(i*2 ^ i)+1:i=i+1:Wend:?j=[A1]
False 

Taylor Scott

Posted 2017-06-17T20:10:02.633

Reputation: 6 709

1

Swi-Prolog, 69 bytes

f(X) succeeds if it can find a value I where X = I*2^I+1. The range hint stops it running out of stack space, but it's enough for the range of Cullen numbers up to 10^9 in the question spec.

:-use_module(library(clpfd)).
f(X):-I in 0..30,X#=I*2^I+1,label([I]).

e.g.

f(838860801).
true

TessellatingHeckler

Posted 2017-06-17T20:10:02.633

Reputation: 2 412

1

cQuents, 9 bytes

$0?1+$2^$

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`

Stephen

Posted 2017-06-17T20:10:02.633

Reputation: 12 293

1

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

calc84maniac

Posted 2017-06-17T20:10:02.633

Reputation: 165

You might want to add an explanation to this. – Gryphon – 2017-08-01T23:58:07.243

Done, thanks for the tip. – calc84maniac – 2017-08-02T00:12:48.293

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.

– Gryphon – 2017-08-02T00:24:42.147

Your welcome. I remember when I first joined the site, people told these types of things to me. Just passing on the favour. – Gryphon – 2017-08-02T00:32:51.343

0

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.

steenbergh

Posted 2017-06-17T20:10:02.633

Reputation: 7 772

0

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.

zgrep

Posted 2017-06-17T20:10:02.633

Reputation: 1 291

0

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!

Bashful Beluga

Posted 2017-06-17T20:10:02.633

Reputation: 413

0

Pari/GP, 25 bytes

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

Try it online!

alephalpha

Posted 2017-06-17T20:10:02.633

Reputation: 23 988