Problem of the Day
A new programming or logic puzzle every Mon-Fri

Double Up

To round out our final problem for November we'll get a little mathy.

What is the smallest integer greater than 0 that when you shuffle the numbers it becomes twice the original number. Thus if your number was 12 you could shuffle to 21 but it wouldn't be twice 12.

Good luck and enjoy your weekend!

Permalink: http://problemotd.com/problem/double-up/

Comments:

  • PyBanana - 10 years ago

    I'm not sure if I got it right but I got: 125874

    public class DoubleUp {
        public static void main(String[] args) {
            int x = 0;
            do {
                x++;
            } while (!isDoubleUp(x));
            System.out.println("Smallest: " + x);
        }
        public static boolean isDoubleUp(int num) {
            return shuffle("", Integer.toString(num), num);
        }
        public static boolean shuffle(String soFar, String rest, int num) {
            if (rest.length() == 0) {
               return Integer.parseInt(soFar) == 2 * num;
            }
            for (int i = 0; i < rest.length(); i++) {
                if (shuffle(soFar + rest.substring(i, i+1), rest.substring(0,i) + rest.substring(i + 1), num)) {
                    return true;
                }
            }
            return false;
        }
    }
    

    reply permalink

  • Anonymous - 10 years ago

    Got the same number 125874

    public class Program
    {
        static char[] numberArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
        static void Main(string[] args)
        {
            int i = 1;
            while (!IsShuffable(i.ToString().ToCharArray(), (i *2).ToString().ToCharArray()))
            {
                i += 1;
            }
            Console.WriteLine("The lowest number is " + i);
        }
    
    
        public static bool IsShuffable(char[] firstNumber, char[] secondNumber)
        {
            bool isShuffable = true;
    
            if (firstNumber.Length != secondNumber.Length)
            {
                isShuffable = false;   
            }
    
            for (int i = 0; i < numberArray.Length && isShuffable ; i++)
            {
                if (firstNumber.Where(j => j == numberArray[i]).Count() != secondNumber.Where(k => k == numberArray[i]).Count())
                {
                    isShuffable = false;
                }
            }
    
            return isShuffable;
        }
    }
    

    reply permalink

  • Jignesh Jain - 10 years ago

    Ans is 125874 (shuffle -> 251748)

    PS: didn't knew how to solve it, so bruteforced it :P

    #include <bits/stdc++.h>
    using namespace std;
    
    bool solve(int n){
        string s = to_string(n);
        while(next_permutation(s.begin(),s.end())){
            int x = stoi(s);
            if(x==2*n)return 1;
        }
        return 0;
    }
    
    int main(){
        for(int i=1;;i++)
            if(solve(i)){
                cout<<"Smallest: "<<i<<endl;        
                break;
            }
    }
    

    reply permalink

Content curated by @MaxBurstein