Apocalyptic numbers

Find the least n n such that the number 2 n 2^n contain 3 consecutive 6's, " 666 666 ".


The answer is 157.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Arulx Z
Jan 23, 2016
1
2
3
4
5
6
7
8
>>> n = 2
>>> count = 1
>>> while '666' not in str(n):
        n *= 2
        count += 1

>>> print count
157

i really like your solutions ^^

Abdeslem Smahi - 5 years, 4 months ago

Log in to reply

Thanks a lot :)

Arulx Z - 5 years, 4 months ago
David Holcer
Mar 17, 2016
1
2
3
4
5
6
7
c=0
while True:
    num=str(2**c)
    if '666' in num:
        print c
        break
    c+=1

Bill Bell
Jan 23, 2016

Avoiding the use of strings and of exponentiation, both of which are expensive.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def threeConsecutive(k,targetDigit=6,targetCount=3):
    found=0
    while k:
        digit,k=k%10,k/10
        if digit==targetDigit:
            found+=1
        else:
            found=0
        if found==targetCount:
            return True
    return False

n=1
consider=2
while True:
    consider*=2
    n+=1
    if threeConsecutive(consider):
        print n, consider
        break

I do agree that string conversion is expensive but for weird reasons, it's actually faster.

Arulx Z - 5 years, 4 months ago

Log in to reply

Ah, very interesting. I must try timing it sometime.

Bill Bell - 5 years, 4 months ago

Log in to reply

Here are the results -

1
1000 loops, best of 3: 977 usec per loop

1
10000 loops, best of 3: 46.4 usec per loop

Arulx Z - 5 years, 4 months ago

Log in to reply

@Arulx Z Here are some of my timings too.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
>pythonw -u "time strings.py"
157 182687704666362864775460604089535377456991567872
timing: 0.00600004
157 182687704666362864775460604089535377456991567872
timing: 0.00100017
>Exit code: 0
>pythonw -u "time strings.py"
157 182687704666362864775460604089535377456991567872
timing: 0.00500011
157 182687704666362864775460604089535377456991567872
timing: 0.00099993
>Exit code: 0
>pythonw -u "time strings.py"
157 182687704666362864775460604089535377456991567872
timing: 0.00499988
157 182687704666362864775460604089535377456991567872
timing: 0.00099993
>Exit code: 0

In each case the first timing is for my original code, the second using equivalent string code. You're absolutely right. I'm old enough (69 in less than a week's time) that I should know never to depend on my intuitions.

Bill Bell - 5 years, 4 months ago

Here is my code (Python 3):

def iscons(n):
    for i in range(0,len(str(n))-2):
        if int(str(n)[i])==6 and int(str(n)[i+1])==6 and int(str(n)[i+2])==6:
            return True
        else:
            pass
    return False

n=1
while n>0:
    if iscons(2**n)==True:
        print (n)
        break
    else:
        n+=1

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...