Hey guys can you help with this basic programming algorithm? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hey guys can you help with this basic programming algorithm?

Develop an algorithm to print all prime numbers between 1 and N. Where N is an integer value less than or equal to 1000. You can use either pseudo code or flow chart. Attempt: START Get N If 1 >= N > 1000 Print False For i=2, i<N, i++ If N%i==0 Print False For i=2, i<=N, i++ If i is Prime Print i END

1st May 2020, 10:51 AM
Michael Gidey
Michael Gidey - avatar
16 Answers
+ 3
The concept looks like as follows: isPrime= true; for(int i=2;i<=num/2;i++) { temp=num%i; if(temp==0) { isPrime=false; break; } }
1st May 2020, 11:19 AM
JaScript
JaScript - avatar
+ 2
I did some rewrite of your algorithm, say if I correctly guess your though (because that's not obvious^^): START Get N If 1 >= N > 1000 Print False endIf For i=2, i<N, i++ If N%i==0 Print False endIf endFor For i=2, i<=N, i++ If i is Prime Print i endIf endFor END Anyway, why these "print false" ? And your task is to "print all prime numbers between 1 and N", isn't it? Are you sure that you don't have to explicit the "is Prime"? Because if so, for what I understand, it's enough to do: START GET N FOR (X=1; X<=N; ++X) IF X IS PRIME PRINT X END IF END FOR END That seems to me very too much simple to be what's expected ;P
2nd May 2020, 6:30 PM
visph
visph - avatar
+ 1
Kiibo Ghayal Ok i appreciate that..i have solved some related problems. But this one is a bit difficult. I will try and come up with what something.
1st May 2020, 10:58 AM
Michael Gidey
Michael Gidey - avatar
+ 1
Michael Gidey algortithm is algorithm, by essence it doesn't involve high nor low level programming language ^^ ... buy don't care: you make me laugh, and I was just sharing that... I can understand ;) Where do you stuck exactly? At the prime numbers generation or at the formating of the result?
1st May 2020, 4:56 PM
visph
visph - avatar
+ 1
Are you really sure that you need to write the "algorithm" of just iterating plus checking the user entry rather than the one to determine if a number is prime? I intentionally I not done user entry check, as that seems to me not an "algorithm" task... but if you want: START GET N IF N%1 OR N<1 OR N>1000 ERROR END IF ... Michael Gidey who give you or where did you find this exercise? :o [edited] it seems as much obvious, that I forgotten the greater than 1000 case :P
2nd May 2020, 6:54 PM
visph
visph - avatar
0
Michael Gidey :D You've titled your question with "basic programming algorithm", and finally you qualify it as "a bit difficult" ;P
1st May 2020, 4:45 PM
visph
visph - avatar
0
What i meant by basic is, we are supposed to use pseudo code or flow chart only. Not high level programing language.
1st May 2020, 4:50 PM
Michael Gidey
Michael Gidey - avatar
0
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 can you review my answer to the above question? START Get N If 1 >= N > 1000 Print False For i=2, i<N, i++ If N%i==0 Print False For i=2, i<=N, i++ If i is Prime Print i END
2nd May 2020, 6:15 PM
Michael Gidey
Michael Gidey - avatar
0
Thanks Visph, i think you r right. But N should be an integer b/n 1 and 1000....shouldn't you specify that?
2nd May 2020, 6:47 PM
Michael Gidey
Michael Gidey - avatar
0
Its a class assignment...im a 1st year student of computer science
2nd May 2020, 7:03 PM
Michael Gidey
Michael Gidey - avatar
0
Could you copy-paste the EXACT words of the assignement, please? I really doubt that your task should limit to that ^^
2nd May 2020, 7:05 PM
visph
visph - avatar
0
It is the exact question i posted here. Word by word
2nd May 2020, 7:09 PM
Michael Gidey
Michael Gidey - avatar
0
And who did tell you that you doesn't need to explicit the "is prime" part?
2nd May 2020, 7:10 PM
visph
visph - avatar
0
Develop an algorithm to print all prime numbers between 1 and N. Where N is an integer value less than or equal to 1000. You can use either pseudo code or flow chart.
2nd May 2020, 7:10 PM
Michael Gidey
Michael Gidey - avatar
0
It could be something like this. 1. Define variable n 2. Define array that contains 2, 3 and 5. 3. Define residue 4. Define a for with an index, that goes from 1 to 1000. >Inside for 5. Set the value of the index to the n variable 6. Define a second for with an index with a size for the index of the array length -1. >Inside for 7. Set the value of the division of the n variable with the array value indicate by the index position to the residue variable. 8. Define an if that compares the residue result module with one 9. If is true, then print n I dont remember how this should be write. I hope it helps you.
3rd May 2020, 5:59 AM
David Jaramillo
David Jaramillo - avatar
- 1
Tnx David
3rd May 2020, 11:22 AM
Michael Gidey
Michael Gidey - avatar