Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*we need to multiply our first digit by 2
- until we get a 2 with no carry over.
- When we have a multiplication that exceeds 9,
- we add the remainder to the digit and add 1 to the next
- multiplication.*/
- public class Solution {
- public static void main(String[] args){
- int b=2;
- boolean remainder=false;
- StringBuilder number = new StringBuilder("2");
- do{
- if (!remainder) {
- b *= 2;
- } else {
- b = b * 2 + 1;
- }
- if (b < 10) {
- number.append(b);
- remainder = false;
- } else {
- number.append(b%10);
- b %= 10;
- remainder = true;
- }
- if(b/2==1 && b%2==0 && !remainder){break;}
- }while(true);
- String new_number = number.reverse().toString();
- new_number=new_number.substring(1,number.length());
- long result = Long.parseLong(new_number);
- System.out.println(result);
- }
- }
Add Comment
Please, Sign In to add comment