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
The discussion focuses on creating an algorithm to determine if a number N is prime. The initial approach involves checking divisibility by all integers up to N, which is deemed inefficient and unnecessarily lengthy. Participants suggest optimizing the algorithm by only checking divisibility up to the square root of N and using established methods like the Sieve of Eratosthenes. There is also criticism of using "goto" statements in pseudocode, advocating for clearer conditional and looping structures instead. Overall, the conversation emphasizes the importance of efficiency and clarity in algorithm design.
  • #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
4K
  • · 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
567
  • · Replies 39 ·
2
Replies
39
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K