Using a prime number - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Using a prime number

Posts: 1Member
edited October 2013 in Java
[b][red]This message was edited by goldleo at 2004-8-1 15:34:38[/red][/b][hr]

How I can display a list of prime number using a loop?

Tagged:

• Posts: 2,914Member
: Can someone show me how can I find out a list of number up to 1000 in java in applet that find out whether the number is prime number or not? And just print out only the list of prime number on the applet.
:
I've got a class that does this on my workstation, but that's at the office and I'm at home. Basically you use the sieve method. That is, get an array with elements 0 to 999, populate it with numbers 1 to 1000. Then:-

1) Let the first element be zero.
2) Create a position variable, e.g. called Pos. Set it to 1.
3) Starting from the Pos element, look for a non-zero value. When you find one, loop through the rest of the array, setting every value that is a multiple of that number (but not that number itself) to 0. Increment Pos.
4) Keep going back to 3 until you've reached the end of the array.

Then you just print every non-zero element of the array. By way of example, if you had an array of numbers 1 to 10 here's how the values would be for each iteration:-

0 2 3 4 5 6 7 8 9 10
0 2 3 0 5 0 7 0 9 0
0 2 3 0 5 0 7 0 0 0
0 2 3 0 5 0 7 0 0 0
0 2 3 0 5 0 7 0 0 0

Hope this helps,

Jonathan

###
for(74,117,115,116){\$::a.=chr};((\$_.='qwertyui')&&
(tr/yuiqwert/her anot/))for(\$::b);for(\$::c){\$_.=\$^X;
/(p.{2}l)/;\$_=\$1}\$::b=~/(..)\$/;print("\$::a\$::b \$::c hack\$1.");

• Posts: 67Member