basic prime numbers

This is a discussion on basic prime numbers within the C++ Programming forums, part of the General Programming Boards category; I decided to make a small program that found prime numbers, and after an hour or so of tweaking i ...

basic prime numbers

I decided to make a small program that found prime numbers, and after an hour or so of tweaking i came up with something that was about 30 times faster than my first attempt, however I still have a couple of questions:

1. Is there an alternative to goto
2. How else can I optomise it further/any further improvements?
3. It only really needs to test if it is divisible by prime numbers, not every number, is there a way of doing this without taking up too much memory, and if so what shall i use, and will it even be quicker?

1) In C++ headers inherited from C are named "c+name" and without ".h". Hence: <cmath>

2) Your varaiables shouldn't be global. Actually you should declare them at the point where you can initialize them at the same time.

3) I'm afraid using the increment operator twice like you do may not give the same result with all compilers. If you want to increment by 2, use

Code:

i += 2;

4) Your goto is fine by me if you think this speeds things up. You can make the program faster if you store the primes you have found somewhere and only do trial division with the primes you have found so far (up to the square root).

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

There are algorithms, that can be implemented with 'goto' - and only with 'goto'!

'goto' is a basic jmp command your machine actually is able to understand (literally!).
This is why goto is executed more quickly than most loops, which usually consist of a combination of many machine commands.
So - although you are often told to avoid 'goto' and to use loops instead, 'goto' is the more optimized version in most cases.
But nevertheless it is a good idea to use loops instead of 'goto'.
Ha ha.

>> There are algorithms, that can be implemented with 'goto' - and only with 'goto'!
Like what?

If you're learning C++, then you should be learning about making clear, robust code first and foremost. In most cases the goto will will have no noticable positive impact on execution speed, and in most cases it will have a noticable negative impact on coding speed. That's why beginners are taught to avoid it.

>> There are algorithms, that can be implemented with 'goto' - and only with 'goto'!
Like what?

If you're learning C++, then you should be learning about making clear, robust code first and foremost. In most cases the goto will will have no noticable positive impact on execution speed, and in most cases it will have a noticable negative impact on coding speed. That's why beginners are taught to avoid it.

(Of course) (one) (should use) (loops) instead!
Only wanted to show the positive effects of 'goto'. Most tutorials/books say it's the worst thing on earth to use 'goto' in C++ - programs. GOTO is by far not a useless basic command. There is a good reason, why almost every programming language knows the GOTO command. A language without GOTO is NOT TURING COMPLETE. Never.
Ha ha.