We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<vector>#include<cmath>usingnamespacestd;// Function to calculate the sum of digits of a numberintsumOfDigits(intn){intsum=0;while(n>0){sum+=n%10;n/=10;}returnsum;}// Function to find the prime factors of a numbervector<int>primeFactors(intn){vector<int>factors;for(inti=2;i<=sqrt(n);++i){while(n%i==0){factors.push_back(i);n/=i;}}if(n>1){factors.push_back(n);}returnfactors;}// Function to check if a number is a Smith numberboolisSmithNumber(intn){if(n<2)returnfalse;// Numbers less than 2 are not Smith numbersvector<int>factors=primeFactors(n);if(factors.size()==1){returnfalse;// Prime numbers are not Smith numbers}intdigitSum=sumOfDigits(n);intfactorsDigitSum=0;for(intfactor:factors){factorsDigitSum+=sumOfDigits(factor);}returndigitSum==factorsDigitSum;}intmain(){intn;cin>>n;if(isSmithNumber(n)){cout<<1<<endl;}else{cout<<0<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Identify Smith Numbers
You are viewing a single comment's thread. Return to all comments →