The question is to write a number n as a product of its prime factors in C++
For example 14 = 2*7
24 = 2*2*2*3
5 = 5.
My code is :
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n)
{
for (int i=2;i<=sqrt(n);i++)
{
if (n%i==0) return false;
}
if (prime) return true;
}
int main ()
{
int n;
int k=0,a[10000]={0};
cin>>n;
while (n!=1)
{
for (int i=2;;)
{
if (n%i==0 && prime(i)) {n/=i; a[k]=i;k++; }
else i++;
if (n==1) break;
}
}
cout<<a[0];
int s=1;
while (a[s]!=0)
{
for (s=1;;s++)
{
if (a[s]==0) break;
cout<<"*"<<a[s];
}
}
return 0;
}
But the problem is time limit and the compiler_stderr.txt shows me this message:
solver.cpp: In function `bool prime(int)':
solver.cpp:11: warning: the address of `bool prime(int)', will always evaluate as `true'
And when I enter 2147483647 it shows me this number again but after 5 or 6 seconds