All roads lead to Rome, all numbers lead to Binary

Every positive integer has a multiple that, when expressed in base ten, is comprised entirely of zeros and ones. Finds the smallest multiple of 98961 that has this property.


The answer is 1110010010001.

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.

10 solutions

Kenny Lau
Oct 11, 2014

I check through all the numbers containing only zeros and ones by converting all positive integers into binary.

Java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class brilliant201410111913{
    public static void main(String[] args){
        for(int i=1;;i++){
            if(Long.parseLong(Integer.toBinaryString(i))%98961==0){
                System.out.print("    "+Integer.toBinaryString(i)+"\n    ");
                break;
            }
        }
    }
}

Output:

1110010010001
Press any key to continue . . .

Nice solution! That's how I did it too.

Michael Ng - 6 years, 7 months ago
Thaddeus Abiy
Oct 6, 2014

98961 × i = n 98961 \times i = n

If n n is the integer composed of only 1 1 's and 0 0 's, then n n must equal 1 1 or 0 0 mod 10 10 . The values of i i that allow this are themselves 1 1 or 0 0 mod 10 10 . So the only values of i i we would have to check are 10 k 10k or 10 k + 1 10k + 1 . This cuts our search space by 10 10 . An implementation using this idea in Java.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class rome {
    public static boolean is_one_zero(long n) {
        // Returns true if n is all 0s and 1s

        while (n != 0) {
            if (n % 10 > 1)
                return false;
            else
                n /= 10;
        }
        return true;
    }

    public static void main(String[] args) {
        long i = 1;
        while (true) {
            if (is_one_zero(98961 * i * 10)) {
                System.out.println(98961 * i * 10);
                break;
            } else if (is_one_zero(98961 * i * 10 + 1)) {
                System.out.println(98961 * i * 10 + 1);
                break;
            }
            i++;

        }
    }

}

It takes time :/

Agnishom Chattopadhyay - 6 years, 8 months ago

Log in to reply

It sure does.. ~5s was what was on top of my head. Nice solution though also nice status ..

Thaddeus Abiy - 6 years, 8 months ago

I thought you would always post your answer only in Python?

Christopher Boo - 6 years, 8 months ago

Log in to reply

Incidentally I am learning java in this course I am taking, so I thought your prob would be great practice

Thaddeus Abiy - 6 years, 8 months ago

In your face xp (look at my solution)

Kenny Lau - 6 years, 8 months ago

Log in to reply

Haha..Good for you.. upvoted

Thaddeus Abiy - 6 years, 8 months ago

Log in to reply

Thanks for your upvote :)

Kenny Lau - 6 years, 8 months ago
Aditya Mishra
Oct 7, 2014

i used a simple heuristic to speed up iterations.

first of all, let us assume that we have to multiply 'c' to 98961 to get the solution. now, the unit digit of 'c' has got to be 1 because if it's 0, we can trim it down to get a smaller multiplier. since unit digit of 'c' is 1, tens digit of 'c' must be 4 or 5 to keep the tens digit of product 0 or 1 correspondingly.

hence iterate by using a multiplier c which increments by 10 if it's tens digit is 4 and by 90 it it's tens digit is 5. thus the last two digits of c remain 41 and 51. we can continue with this reasoning but even this much gives a fast enough solution (within 0.047 seconds on my PC).

Here is the c++ code,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#define ll long long int
using namespace std;
bool binary(ll x)
{
    while(x>0)
    {
        if((x%10)>1)
            return false;
        x/=10;
    }
    return true;
}
int main()
{
    ll x=98961;
    ll b=x;
    ll c=41;
    int t1,t2;
    while(!binary(x))
    {
        t1=c/10;
        t2=t1%10;
        if(t2==4)
            c+=10;
        else
            c+=90;
        x=b*c;
    }
    cout<<x<<endl;
    return 0;
} 

edited. you can click the "edit" button to view what I have changed.

Kenny Lau - 6 years, 8 months ago

Log in to reply

Thank you for the help :)

Aditya Mishra - 6 years, 8 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def plusone(n):
     n = [i for i in reversed(list('0' + str(n)))]
     for i in xrange(len(n)):
             if n[i] == '1':
                     n[i] = '0'
             elif n[i] == '0':
                     n[i] = '1'
                     break
     n = int(''.join(([i for i in reversed(n)])))
     return n

i = 1
while i%98961:
    i = plusone(i)
print i

Fastest algorithm

it is quite fast but i sincerely doubt it is the fastest as it is ultimately a brute force iteration without any regard to the nature of the given number and the restrictions imposed on the number which has to be multiplied to 98961 to get a product as required.

Aditya Mishra - 6 years, 8 months ago

it looks like you brute forced all values of 'i' till 'i' was divisible by number. looking at the length of solution, your algorithm would have finished within around 8000 (~2^13) iteration.

Aditya Mishra - 6 years, 8 months ago

for your solution, one way to make it twice as fast would be to restrict the last digit of i to 1 (for smallest such product, unit digit has to be 1 since last digit of 98961 is 1) and the count of iteration is cut to half.

Aditya Mishra - 6 years, 8 months ago

I also used the fact that 98691 98691 has to multiplied by 10 i 10i and 10 i + 1 10i+1 to have the last digit of the multiple be 0 0 and 1 1 respectively. I used the following Python code.

 i = 1 

 while True:

         s = str(989610*i)

     result = filter(lambda x: x=='0' or x=='1', s)

     if result == s: break

     s = str(98961*(10*i+1))

     result = filter(lambda x: x=='0' or x=='1', s)

     if result == s: break

     i += 1

print s

What is the number?

bobbym none - 6 years, 8 months ago
Arulx Z
May 14, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public static void main(String args[]) {
    DecimalFormat d = new DecimalFormat("#");
    for(double i = 1;; i++)
        if(check(d.format(98961d * i)))
            System.out.println(98961d * i);
}

private static boolean check(String n){
    if(n.contains("2") || n.contains("3") || n.contains("4") || n.contains("5")
    || n.contains("6") || n.contains("7") || n.contains("8") || n.contains("9"))
        return false;
    return true;
}

Vladimir Lem
Mar 6, 2015

I solved this by generating all integers in base 10 containing 0s and 1s, and using BFS algorithm to get the smallest multiple. C++ code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
#define cb(x) (x)*(x)*(x)
using namespace std;
long long num;
queue <long long> bfq; // BFS queue
int main() {
    bfq.push(1); // 
    while (!bfq.empty()) {
        num=bfq.front(); // get the front number
        if (num%98961 == 0) break; // if the number is divisible by 98961, then it is the answer
        else {
            bfq.push(num*10); // add the number with '0' and push it to the queue
            bfq.push((num*10)+1); //add the number with '1' and push it to the queue
        }
        bfq.pop();
    }
    cout << num;
    return 0;
}

Boom, 1110010010001

Bill Bell
Dec 17, 2014

No clever mathematics, easy to code.

Brian Brooks
Dec 1, 2014

Simple brute-force in Python:

def f(x):
    while x:
        if x % 10 != 0 and x % 10 != 1:
            return False
        x = x / 10
    return True

n = 98961
while not f(n):
    n = n + 98961
print n
Nahid Shrabon
Nov 29, 2014

I have solved this problem with C++ using Dynamic Programming(DP). Here's my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<cstring>
using namespace std;

const int mod = 98961, L = 100;
int dp[L][mod + 5];

int solve(int p, int m) {
    if(!m) return 0;

    if(p == L) return 1e9;

    if(dp[p][m] != -1) return dp[p][m];

    dp[p][m] = 1e9;
    dp[p][m] = min(dp[p][m], 1 + solve(p + 1, (m * 10 + 1) % mod));
    dp[p][m] = min(dp[p][m], 1 + solve(p + 1, (m * 10) % mod));

    return dp[p][m];
}

void pp(int p, int m) {
    if(!m || p == L) return;

    if(dp[p][m] == 1 + solve(p + 1, (m * 10 + 1) % mod)) {
        cout << 1;
        pp(p + 1, (m * 10 + 1) % mod);
        return;
    } else if(dp[p][m] == 1 + solve(p + 1, (m * 10) % mod)) {
        cout << 0;
        pp(p + 1, (m * 10) % mod);
        return;
    }
}

int main() {
    memset(dp, -1, sizeof dp);
    cout << "Length: " << solve(1, 1) + 1 << endl;
    cout << "Number: 1";
    pp(1, 1);
    cout << endl;
    return 0;
}

1
2
Length: 13
Number: 1110010010001

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...