Print Prime Numbers 1-100 in Pseudocode

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    comp sci
Click For Summary
SUMMARY

The discussion focuses on the pseudocode for printing prime numbers between 1 and 100. The initial attempt incorrectly handles the primality test logic, particularly in the use of the variable isPrime. It is established that starting the loop at 1 adds unnecessary complexity, and the inner loop should terminate at sqrt(i) instead of 10. The consensus is that the primality testing logic must be corrected to ensure accurate results.

PREREQUISITES
  • Understanding of basic programming constructs (loops, conditionals)
  • Familiarity with pseudocode syntax
  • Knowledge of prime number definition and properties
  • Concept of cyclomatic complexity in programming
NEXT STEPS
  • Revise the pseudocode to implement a correct primality test
  • Learn about the sqrt() function and its application in loops
  • Explore best practices for reducing cyclomatic complexity in code
  • Study the implications of using continue statements in programming
USEFUL FOR

Students learning programming, software developers interested in algorithm optimization, and anyone looking to improve their understanding of prime number generation techniques.

NDiggity
Messages
53
Reaction score
0

Homework Statement


The problem is to find all the prime numbers between one and one-hundred and print them out in pseudocode.

Here is my attempt. The problem is that when it's false, isPrime will be false but when it tries to divide by the next number and it is true, it makes isPrime true. I'm not sure how to fix this. Help!

bool isPrime <-- true
int i
int j

for i <-- 1 to 100
for j <--2 to 10
if (i=1)
continue
if (i =2)
print i
continue
if (i = j)
continue
if (i % j != 0 AND i!=j)
isPrime= true
if (i % j = 0)
isPrime = false
if (isPrime = true)
print i
 
Physics news on Phys.org
Some comments, the big problem is last.

NDiggity said:
for i <-- 1 to 100
Why are you starting at 1, or even 2? You know 1 is not prime and 2 is prime, as you have these hardcoded as exceptional cases. Print that 2 is prime and start at 3. All that those exceptional cases do is add complexity to the program. A lot (an awful lot) of programming shops measure the "cyclomatic complexity" of code. A lot (not quite as many, but still a lot) of programming shops specifically ban the use of "continue".

for j <--2 to 10
Much better would be to end this loop at sqrt(i), or if that is not acceptable, at i-1. Doing either makes a lot more sense than ending at 10.

if (i % j != 0 AND i!=j)
isPrime= true
if (i % j = 0)
isPrime = false
This is the big problem. There are two problems here. First, any number j that divides some other number i means i is not prime. This logic does not test for primality! Another problem: You have a fall through case (i.e., there is no final else). Once again, many programming shops view this as a sign of sloppy programming. Even if a final else is not needed, it is better to put one in that explicitly does nothing. Doing so shows that the programmer has thought about all of the cases.

Bottom line: You really need to fix the primality testing logic. Fixing the loop bounds is a good idea as well.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 80 ·
3
Replies
80
Views
10K