Hello all!! i have to make a program for my university that generates 3000 random numbers and finds the one who are powerful! I have written all of the code but i don't know what condition is wrong! let me show you... :-P

``````for (l=4 ; l < COMPUTATIONS ; l++){
x=l;
m=3;
k=0;
z=x;
while (x%2==0){
x=x/2;
k=k+1;}

while ((m*m<=x) && (k!=1)){
k=0;
while (x%m==0){
x=x/m;
k=k+1;
}
m=m+2;}

if (k>1){
printf ("%d is powerful\n",z);``````

I do this to find the numbers from 4 to 1000 that are powerful and compare them with http://en.wikipedia.org/wiki/Powerful_number
My results:
4 is powerful
8 is powerful
9 is powerful
12 is powerful
16 is powerful
20 is powerful
24 is powerful
25 is powerful
27 is powerful
28 is powerful
32 is powerful
36 is powerful
40 is powerful
45 is powerful
48 is powerful
49 is powerful
56 is powerful
63 is powerful
64 is powerful
72 is powerful
80 is powerful
81 is powerful
96 is powerful
99 is powerful
100 is powerful

.
.
.
.
It seem that my code finds more numbers powerful that aren't!
Any suggestion would be welcome!! Thanks!!

## All 5 Replies

As I understand it there is a simpler way of calculating the powernumbers. All powernumbers are made up of either power of 2 or power of 3 (or am I wrong?).

I wrote the code below to produce 100 powernumbers of wich some are duplicates. Then you can sort the numbers and removing the duplicate ones.

``````#include <iostream>
#include <math.h>

using namespace std;

int myPowerOf( int base, int exponent );

int main ( )
{
int i;
int powNumbers[ 100 ] ;

for( i=0; i<50; i++ )
{
powNumbers[i]    = myPowerOf( i+1, 2 );
powNumbers[i+50] = myPowerOf( i+1, 3 );
}

for( int i=0; i<100; i++ )
{
printf( "\nNum %d: %d", i+1, powNumbers[i]);
}

return 0;
}

int myPowerOf( int base, int exponent )
{
int j;
int tmpVar=base;

for( j=0; j<exponent-1; j++ ){
tmpVar*=base;
}

return tmpVar;
}``````

Forgot to remove
#include <math.h>. It is not needed since I wrote my own pow() function... :)

hmmm it seems right but he doesn't want to use any function because he hasn't teached us yet the fuctions!

anyways i figured it out... it comes away with the right results but i don't know if its right!!
i changed this condition:

``while ((m*m<=x) && (k!=1)){``

with

``while ((m<=x) && (k!=1)){``

and its seems that the results match with the ones at wiki!
and now i made the code to generate 3000 random numbers i come away with a 1% of 3000 that are powerful!
the teacher told us that we want about 1%
so i guess it is solved? :D thanks anyway!