Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Optimized Solution for prime numbers..

Optimized Solution for prime numbers..

Scheduled Pinned Locked Moved ATL / WTL / STL
c++
6 Posts 3 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raunak Singh Chauhan
    wrote on last edited by
    #1

    I have written a program in c++ to check whether a number is prime or not.... I want to know if this is optimized or not.. #include using namespace std; int main() { int num; cout<<"Enter a number:"; cin>>num; if(num%6==1 || num%6==5 || num==2 || num==3) { cout<<"\n"<

    N B 2 Replies Last reply
    0
    • R Raunak Singh Chauhan

      I have written a program in c++ to check whether a number is prime or not.... I want to know if this is optimized or not.. #include using namespace std; int main() { int num; cout<<"Enter a number:"; cin>>num; if(num%6==1 || num%6==5 || num==2 || num==3) { cout<<"\n"<

      N Offline
      N Offline
      Nicolas Dorier
      wrote on last edited by
      #2

      If you start programming, lesson number one : do not ask whether something is optimized or not without having performance target in mind. People that ask you to optimize your code without reason just want to waste your time on trivia. Takes 1 000 000 random numbers. Then measure how much times it takes to find out whether they are prime on your machine. Then see if you can make it better with random changes and observe.

      R 1 Reply Last reply
      0
      • N Nicolas Dorier

        If you start programming, lesson number one : do not ask whether something is optimized or not without having performance target in mind. People that ask you to optimize your code without reason just want to waste your time on trivia. Takes 1 000 000 random numbers. Then measure how much times it takes to find out whether they are prime on your machine. Then see if you can make it better with random changes and observe.

        R Offline
        R Offline
        Raunak Singh Chauhan
        wrote on last edited by
        #3

        I want to know that this program is better than those which are solved using loops.....????

        N 1 Reply Last reply
        0
        • R Raunak Singh Chauhan

          I want to know that this program is better than those which are solved using loops.....????

          N Offline
          N Offline
          Nicolas Dorier
          wrote on last edited by
          #4

          only way to know it is to do what I told you just above. This not only depends on the code but on the compiler. A coder can't tell you without benchamrking.

          R 1 Reply Last reply
          0
          • N Nicolas Dorier

            only way to know it is to do what I told you just above. This not only depends on the code but on the compiler. A coder can't tell you without benchamrking.

            R Offline
            R Offline
            Raunak Singh Chauhan
            wrote on last edited by
            #5

            Ok.... thanks a lot..

            1 Reply Last reply
            0
            • R Raunak Singh Chauhan

              I have written a program in c++ to check whether a number is prime or not.... I want to know if this is optimized or not.. #include using namespace std; int main() { int num; cout<<"Enter a number:"; cin>>num; if(num%6==1 || num%6==5 || num==2 || num==3) { cout<<"\n"<

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              "Optimized" is not the correct category here. It is simply wrong. 25%6=1, but 25 is not prime.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups