Binary Pi, Anyone?

π = 11.001001 \large \pi = 11.001001\dots Write π \pi in binary notation and consider the first 32 digits (i.e. digits after the binary point).

How many of these digits are "1"?

14 15 16 17 18

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.

3 solutions

π = 11.00100100 00111111 01101010 10001000 10000101 10100011 . . . \pi=11.00100100\ 00111111\ 01101010\ 10001000\ 10000101\ 10100011\ ...

Regarding how to do this by programming, here's a function definition written in Python 3 to print the binary representation of a non-negative number ( int or float ) :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def binf(n,d=10):
    k=int(n//1)
    n-=k
    l1=[]; l2=[]
    while k>0:
        l1=[str(k%2)]+l1
        k//=2
    count=1
    while n>0 and count<=d:
        l2+=[str(int(n*2**count))]
        if l2[-1]=='1': n-=2**(-count)
        count+=1
    if l2==[]: l2=['0']
    if l1==[]: l1=['0']
    return ''.join(l1)+'.'+''.join(l2)

Importing pi from math module of py3 (or just using a good enough approximation for π \pi ) and calling binf(pi,32) will return the string of binary representation of pi upto 32 decimal places.

Using the count() method associated with strings in python, we can do the counting of 1s by the python interpreter too instead of counting manually.

Calling binf(pi-int(pi//1),32).count('1') will directly give the answer as 14 14 .

Prasun Biswas - 5 years, 2 months ago
Abdelhamid Saadi
Apr 8, 2016

A shorter solution in python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from math import pi

def solve(a, n):
    "Binary Pi, Anyone?"
    cnt  =  0
    for i in range(n):
        a *= 2
        if  a > 1:
            a -= 1
            cnt += 1 
    return cnt

print("Number of 1: ", solve(pi -3, 32))

Hasmik Garyaka
Oct 19, 2017

Use windows calc, calculate pi 2 *32 and click the bin button. 1100100100001111110110101010001000

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...