Making an Algorithm to Check Whether a Number is a Prime Number

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Algorithm Prime
Click For Summary

Discussion Overview

The discussion revolves around creating an algorithm to determine whether a given number (N) is prime. Participants explore various algorithmic approaches, including the use of loops and flowcharts, while addressing specific steps and potential improvements to the proposed algorithm.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines an initial algorithmic approach but expresses uncertainty about its validity and how to finalize it.
  • Another participant suggests testing the algorithm on specific numbers like 28 and 29 to verify its functionality.
  • Some participants critique the length and complexity of the proposed algorithm, suggesting that it could be simplified by using loops.
  • A later reply introduces the Sieve of Eratosthenes as a more efficient method, emphasizing the need to check only prime divisors up to the square root of N.
  • Concerns are raised about the use of "goto" statements in algorithms, with examples provided to illustrate potential logical issues.
  • Participants discuss the importance of clarity in defining steps within the algorithm and the necessity of identifying specific conditions for prime determination.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness and clarity of the proposed algorithm. There is no consensus on a single approach, as some advocate for simplification while others defend the original structure.

Contextual Notes

Participants note limitations in the algorithm's current form, including unnecessary checks and the potential for confusion with step definitions. The discussion highlights the need for clearer logical flow and the importance of efficient prime-checking methods.

  • #31
I couldn't test it cause the class was in the early morning. But my teacher excepted it so Idk it caused no problem.But for today I have to make 2 more flowcharts...
 
Physics news on Phys.org
  • #32
Arman777 said:
I couldn't test it cause the class was in the early morning. But my teacher excepted it so Idk it caused no problem.But for today I have to make 2 more flowcharts...
It's rather sad if the flowcharts you have to write are a lot less clear than the computer program would be. In this case you really want it to be clear in your program for which values of m the trial division is done. if you have
Python:
for m in range (2, n/2):
Code:
for (m=2; m<= n/2; m++)
that should be clear enough. With your pseudocode, or with a flowchart it's not so clear exactly what values of m are used.
That seems to be the origin of the bug here. It will also make it harder to see that the program can easily be much more efficient.
If you really have to make flowcharts, i would recommend to make and test a correct program first, and then produce a flowchart. It should be easy enough to learn how to put a for, while or if into a flowchart. The flowchart will be no help with making a program unless you want to avoid any higher level code structures.
 
  • #33
willem2 said:
If you really have to make flowcharts, i would recommend to make and test a correct program first,
I don't know ho to write a program we didnt learn about it..But I see your point in general and you are right. Maybe yeah it was the problem. So now I have to create a flowchart to calculate a gcd for two positive integers.

Our flowcharts are rather simple things..thats how we are doing in class..
 
  • #34
I have also questions for my other flowchart..I ll open another thread...
 

Similar threads

  • · Replies 27 ·
Replies
27
Views
5K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K