You are viewing a single comment's thread. Return to all comments →
my attempt using c++ just failed 2 tests cuz of timeouts
#include <iostream> #include <vector> #include <cmath> using namespace std; int main(){ vector<int> prime_numbers; bool isPrime = true; int j; cin >> j; int j1 = j; while(j %2 == 0){ prime_numbers.push_back(2); j/=2; } j = j1; while(j%3 == 0){ prime_numbers.push_back(3); j/=3; } j=j1; for(int n = 5;n<= j;n++){ isPrime = true; for(int i = 2;i<=pow(n,0.5);i++){ if(n % i == 0){ isPrime = false; break; } } if(isPrime){ while(j%n == 0){ prime_numbers.push_back(n); j/=n; } j = j1; } } for(int k = 0;k<prime_numbers.size();k++){ if (to_string(prime_numbers[k]).length()>=2){ int sum = 0; string num = to_string(prime_numbers[k]); for(int p = 0;p<to_string(prime_numbers[k]).length();p++){ sum += (num[p] - '0'); } prime_numbers[k] = sum; } } int sum1; for(int w = 0;w<prime_numbers.size();w++){ sum1 += prime_numbers[w]; } string numb = to_string(j); int sum2 = 0; for(int y = 0;y<numb.length();y++){ sum2 += (numb[y] - '0'); } if(sum1 == sum2){ return 1; } else { return 0; } }
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 →
my attempt using c++ just failed 2 tests cuz of timeouts