Stelios_Gakis

ABCD == DABC * 2

Sep 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. /*we need to multiply our first digit by 2
  2. until we get a 2 with no carry over.
  3.  
  4. When we have a multiplication that exceeds 9,
  5. we add the remainder to the digit and add 1 to the next
  6. multiplication.*/
  7.  
  8. public class Solution {
  9.     public static void main(String[] args){
  10.         int b=2;
  11.         boolean remainder=false;
  12.         StringBuilder number = new StringBuilder("2");
  13.         do{
  14.                 if (!remainder) {
  15.                     b *= 2;
  16.                 } else {
  17.                     b = b * 2 + 1;
  18.                 }
  19.                 if (b < 10) {
  20.                     number.append(b);
  21.                     remainder = false;
  22.                 } else {
  23.                     number.append(b%10);
  24.                     b %= 10;
  25.                     remainder = true;
  26.                 }
  27.             if(b/2==1 && b%2==0 && !remainder){break;}
  28.         }while(true);
  29.         String new_number = number.reverse().toString();
  30.         new_number=new_number.substring(1,number.length());
  31.         long result = Long.parseLong(new_number);
  32.         System.out.println(result);
  33.     }
  34. }
Add Comment
Please, Sign In to add comment