CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Threaded View

  1. #11
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Prime Numbers Help?

    i write the code for you:

    Code:
    #include <cstdlib>
    #include<iostream>
    #include<math.h>
    
    using namespace std;
    
    int main() {
        int i, j;
        for(i=2; i<100; i++) 
        {
            bool isPrime = true;
            for(j=2; j<=sqrt(i); j++) 
            {
                if(i&#37;j ==0 ) 
                {
                    isPrime = false;
                    break;
                    
                }
               
            }
            
            if (isPrime)
            {
               cout<< i << " : Is Prime" << endl;          
            }else
            {
               cout<< i << " : Is not Prime" << endl;      
            }
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    according to a math theory it is just enough to test for 0 remainder till reaching sqrt(i). so i used that theory to have faster algorithm
    Last edited by toraj58; April 14th, 2009 at 07:54 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured