How can you solve this input "147573952589676412928" programmatically?


#1

My logic is not working for this input:

But the answer is there 2^67

My CODE:

public int power(String A) {
    int len = A.length();
    if(len==1 && A.charAt(0)=='0') {
        return 0;
    }
    while((!A.equals("1")) && A.charAt(A.length()-1)%2==0) {
        A = divide(A,2);
    }
    if(A.equals("1")){
        return 1;
    }
    else {
        return 0;
    }
}    

public String divide(String s, int val) {
    String carry = "";
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<s.length();i++) {
        int curr = Integer.parseInt(carry+Character.toString(s.charAt(i)));
        if(curr%2!=0){
            carry = "1";
        }
        else {
            carry = "0";
        }
        if(i!=s.length()-1) {
            if(curr/2!=0) {
                sb.append(Integer.toString(curr/2));
            }
        }
        else {
            sb.append(Integer.toString(curr/2));
        }
        
    }
    return sb.toString();
}

#2

string num_str(int n) //number to string
{
stringstream ss;
ss<<n;
return ss.str();
}

string divide(string s)
{
int i=0;
int carry=0;
string y;
int num;
for(i=0;i<s.size();i++) //toremoveleading 0
{
if(s[i]!=‘0’)
{
break;
}
}
for(i=i;i<s.size();i++)
{
num=(s[i]-‘0’);
num=(carry*10)+num;
if(num==0)
{
carry=0;
y=y+“0”;
}
else
if(num%2==0)
{
carry=0;
num=num/2;

        y=y+num_str(num);
        
    }
    else
    {
        carry=1;
        num=num/2;
        
        y=y+num_str(num);
        
    }
    
}
return y;

}

int Solution::power(string A) {

if(A=="1")
{
    return 0;
}
//cout<<A[A.size()-1];
int flag=true;
while((A[A.size()-1]=='8')||(A[A.size()-1]=='6')||(A[A.size()-1]=='4')||(A[A.size()-1]=='2')||(A[A.size()-1]=='0'))
{ //  cout<<"1";
    if((A.size()==1)&&(A[0]=='1'))
    {//cout<<"2";
        break;
    }
    //cout<<"3";
    A=divide(A);
    //A=remove_l(A);
//    cout<<A<<"\n";
}
if((A[0]=='1')&&(A.size()==1))
{
    return 1;
}
else
{
    return 0;
}

}


#3

Try for cases 2048 or 4096, your test case might for these cases also.