teasy

how I find one number if it is the primary number or not by recursion method in C++ language

Re: Visual C++ General primary numbers finding

TilakGopi

Hi,

Use the following function

bool IsPrime(int num)

{

for(int index = 2;index < =num/2;index++)

if(num%index == 0)

return false;

return true;

}

 

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ General primary numbers finding

TilakGopi

Hi teasy,

It seems ur time is prime.

Is my lsPrime() functioning properly

Cheers,

Ch.T.Gopi Kumar.






Re: Visual C++ General primary numbers finding

teasy

thanks your answer but This answer that I want to find ,is not. My question should be solved by recursive function (it is not necessery working with for loop)





Re: Visual C++ General primary numbers finding

Keyu

call function as

IsPrime(num,2)

pass value of ur number here

bool IsPrime(int num,int index)

{

if(index < = num%2 )

if(num%index != 0)

{

index++;

IsPrime(num,index);

return false;

}

return true;

}

ok ..reply m if any probelm

byeee






Re: Visual C++ General primary numbers finding

TilakGopi

Oh,

 I couldn't get ur problem clearly.Sorry.Anyways ,here are 2 new answers.Have ur choice.

---------------------------------------------------------

//Method1

bool IsPrime(int num)

{

static int index = 2;

if(index>(num/2))

{

index = 2;

return true;

}

if(num%index == 0)

{

index = 2;

return false;

}

++index;

return IsPrime(num);

}

//Method2

/*bool IsPrime(int num,int index)

{

if(index>(num/index)) return true;  

if(num%index == 0) return false;

return IsPrime(num,++index);

}*/

int _tmain(int argc, _TCHAR* argv[])

{

for(int index = 2;index <1000;index++)

if(IsPrime(index) )

//or

//if(IsPrime(index,2))  //If u have chosen Method2

cout<<index<<" ";

}

-----------------------------------------

 

Cheers,

Ch.T.Gopi Kumar.






Re: Visual C++ General primary numbers finding

Damien Watkins - MSFT

Hello

Re question:

Such questions are outside the scope of this forum - for the scope of the VC General forum please look at: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=19445&SiteID=1 specifically where it says: "For general C++ questions or general development questions (unrelated to Microsoft implementation issues or features), please use your favorite general C++ or development forums or newsgroups.

OTP

Thanks

Damien