Boston Numbers (Please Help Me To Debug two testcases are failing)

#include
#include<bits/stdc++.h>
#define l unsigned long long int
using namespace std;

l primefactors(l n){
l ans = 0;
while(n % 2 == 0){
ans = ans + 2;
n = n/2;
}

for(l i=3;i<=sqrt(n);i++){
	while(n%i == 0){ 
		n = n/i;
		ans += i;
	}
}
if(n > 2){
	ans+=n;
}
return ans;

}

l BostonNumber(l n){
l ans = 0;
l digitsum = 0;
l temp = n;
while( temp > 0){
l x = temp % 10;
digitsum += x;
temp = temp / 10;
}
l value = primefactors(n);
if(value == digitsum){
return 1;
}
return ans;
}

int main(){
l n;
cin>>n;
cout<<BostonNumber(n)<<endl;
return 0;
}

@amangoel987357
You also have to take the sum of digits of prime factors as well. It is mentioned in the question.

// I have taken that into consideration

#include
#include<bits/stdc++.h>
#define l unsigned long long int
using namespace std;

l primefactors(l n){
l ans = 0;
while(n % 2 == 0){
ans = ans + 2;
// cout<<2<<" ";
n = n/2;
}

for(l i=3;i<=sqrt(n);i++){
	while(n%i == 0){ 
		l temp = i;
		while(temp > 0){
			l temp2 = i % 10;
			ans+= temp2;
			temp = temp / 10;
		}
		n = n/i;

// ans += i;
}
}
if(n > 2){
l temp = n;
while(temp > 0){
l temp2 = temp %10;
ans+= temp2;
temp = temp / 10;
}

}
return ans;

}

l BostonNumber(l n){
l ans = 0;
l digitsum = 0;
l temp = n;
while( temp > 0){
l x = temp % 10;
digitsum += x;
temp = temp / 10;
}
l value = primefactors(n);
// cout<<value<<endl;
// cout<<digitsum<<endl;
if(value == digitsum){
return 1;
}
return ans;
}

int main(){
l n;
cin>>n;
cout<<BostonNumber(n)<<endl;
return 0;
}


I have fixed the bug. Try to write modular code. It helps in debugging.

It seems as if you have written your own code & you are sending me that please add a comment in the line in my code where you made changes. I don’t want to look at your way of coding just give a comment line in my code where there is a mistake.

I just added a sum function in the first code that you gave me. That means you were not taking sum of the digits properly.

1 Like