#include "prime_factors.h"
#include <iostream>
#include <iostream>
using namespace std;
namespace prime_factors {
bool is_prime( int n ) {
for (auto i = 2 ; i <= n/2 ; i++ )
if (n % i == 0) return false;
return true;
}
vector<int> primes(int n) {
vector<int> res;
for (auto i = 2; i <= n ; i++)
if (is_prime(i)) {
res.push_back(i);
}
return res;
}
void display( vector<int> v) {
cout << "vector<int>{";
for (auto it = v.begin() ; (it != v.end()) ; it++ )
if (it == v.begin())
cout << *it ;
else
cout << ", " << *it;
cout << "}" << endl;
}
vector<int> of(int n) {
vector<int> res, t = primes(n);
int div;
if (n < 2) return {};
if (is_prime(n)) return {n};
for (auto it = t.begin() ; (n > 1) && (it != t.end()) ; it++ )
{
div = *it;
while ( n % div == 0)
{
res.push_back(div);
n /= div;
}
}
return res;
}
}; // namespace prime_factors
This c++ code is not funtionning under the in-browser compiler, but does function locally
And here's the header file :
#if !defined(PRIME_FACTORS_H)
#define PRIME_FACTORS_H
#include <vector>
using namespace std;
namespace prime_factors {
vector<int> of(int);
} // namespace prime_factors
#endif // PRIME_FACTORS_H