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

- 141.2K All Categories
- 103.9K Programming Languages
- 6.5K Assembler Developer
- 1.9K Basic
- 40K C and C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.7K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 553 Python
- 37 Ruby
- 4.4K VB.NET
- 1.6K VBA
- 20.9K Visual Basic
- 2.6K Game programming
- 317 Console programming
- 93 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 748 Computer Hardware
- 3.5K Database & SQL
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 258 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 199 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 605 Jobs Wanted
- 213 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 3.4K Miscellaneous
- 7 Join the Team
- 356 Comments on this site
- 71 Computer Emulators
- 2.1K General programming
- 188 New programming languages
- 641 Off topic board
- 226 Mobile & Wireless
- 98 Android
- 126 Palm Pilot
- 340 Multimedia
- 156 Demo programming
- 184 MP3 programming
- Bash scripts
- 28 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 371 MS-DOS
- Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 944 Software Development
- 417 Algorithms
- 68 Object Orientation
- 92 Project Management
- 95 Quality & Testing
- 271 Security
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 62 AJAX
- 6 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 37 JQuery
- 310 WEB Servers
- 157 WEB-Services / SOAP

RodeoBurnz
Member Posts: **47**

in Java

hi I'm trying to write code that will print out all the prime numbers between 2 and 100. I can't get it to work correctly, though. Could anyone please help me? My code is below:

public class Primes {

public int prime;

public Primes() {

}

public void isPrime(){

System.out.println("PRIMES: ");

for (int prime = 2; prime <= 100; prime++){

if ( prime%prime == 0 )

break;

else

System.out.print(prime + " ");

}

}

}

public class Primes {

public int prime;

public Primes() {

}

public void isPrime(){

System.out.println("PRIMES: ");

for (int prime = 2; prime <= 100; prime++){

if ( prime%prime == 0 )

break;

else

System.out.print(prime + " ");

}

}

}

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven

© 1997-2017 Programmersheaven.com - All rights reserved.

## Comments

662[code]

public class Temp extends JFrame

{

public static void main (String[] arg)

{

for (int i = 2; i < 100; i++)

for (int j = 2; j < i; j++)

{

if (i % j == 0)

break;

else if (i == j + 1)

System.out.print (i + " ");

}

}

}

[/code]

Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com

662* Prime numbers up to 100 are the same as prime numbers up to square root of 100. Use [green]Math.sqrt (100)[/green] in the for-loop.

* You don't need to check even numbers (there's only one even prime number and it's 2). Use [green]i += 2[/green] as the increase for i.

* Once you've found a prime number (let's say 5), you can remove all powers of it. For that you need a boolean array and it's probably nothing you should worry about right now :-).

Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com

1,666:

: * Prime numbers up to 100 are the same as prime numbers up to square root of 100. Use [green]Math.sqrt (100)[/green] in the for-loop.

:

: * You don't need to check even numbers (there's only one even prime number and it's 2). Use [green]i += 2[/green] as the increase for i.

:

: * Once you've found a prime number (let's say 5), you can remove all powers [red][you mean multiples (though powers is technically also correct, just not as strict)][/red]of it. For that you need a boolean array and it's probably nothing you should worry about right now :-).

:

:

: Kind Regards

: Konrad

: ----------------------------

: (+46/0) 708-70 73 92

: chamster@home.se

: http://konrads.webbsida.com

:

:

"We can't do nothing and think someone else will make it right."

-Kyoto Now, Bad Religion

662Are you sure? Have you tested? (Especially for large numbers :-))

It's very natural to believe that it's multiples that should be removed, but i believe it's powers. How would you argue for your theory? (Is it [b]argue[/b] or [b]argument[/b], by the way?)

Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com

47RodeoBurnz

: : you can remove all powers [red][you mean multiples (though powers is technically also correct, just not as strict)][/red]

:

: Are you sure? Have you tested? (Especially for large numbers :-))

: It's very natural to believe that it's multiples that should be removed, but i believe it's powers. How would you argue for your theory? (Is it [b]argue[/b] or [b]argument[/b], by the way?)

:

:

: Kind Regards

: Konrad

: ----------------------------

: (+46/0) 708-70 73 92

: chamster@home.se

: http://konrads.webbsida.com

:

:

1,666:

: RodeoBurnz

:

: : : you can remove all powers [red][you mean multiples (though powers is technically also correct, just not as strict)][/red]

: :

: : Are you sure? Have you tested? (Especially for large numbers :-))

: : It's very natural to believe that it's multiples that should be removed, but i believe it's powers. How would you argue for your theory? (Is it [b]argue[/b] or [b]argument[/b], by the way?)

: :

I don't need to argue theory, it's true by definition. By definition a prime number has no factors (other than 1 and itself). By definition a multiple of n has n as a factor. The only multiple of n that can POSSIBLY be prime is the "first". I believe you've mistaken the words power and multiple (though I have no idea what you think multiple means in that case). Powers is also correct, but all (integral) powers of n are also multiples of n (again by definition).

"We can't do nothing and think someone else will make it right."

-Kyoto Now, Bad Religion

662What you seem to be missing is that it's not the fastest method. You will achieve a better performance if you only remove powers of a found prime number (as for 3 it would be 9, 27, 81, 343,...). If you don't believe me - try it yourself. It's specially clear for large numbers.

Can you see how come? (The answer is pretty obvious, when you think of it. Yet, it took me a while before i saw how that's possible.)

Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com

662Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com

1,666:

: What you seem to be missing is that it's not the fastest method. You will achieve a better performance if you only remove powers of a found prime number (as for 3 it would be 9, 27, 81, 343,...). If you don't believe me - try it yourself. It's specially clear for large numbers.

:

: Can you see how come? (The answer is pretty obvious, when you think of it. Yet, it took me a while before i saw how that's possible.)

:

:

What do you do when you reach a composite number that's not a power of a prime? The first odd one is 15. That's not a power of three and not a power of five (and definitely not a power of any other prime). Do you divide it out? Then what about 3*5^50?

Anyways, if going by powers was faster (and still complete) then you wouldn't be using i+=2 you'd be using i*=2.

* Prime numbers up to 100 are the same as prime numbers up to square root of 100. Use Math.sqrt (100) in the for-loop.

Um could you reword this. It sounds like all the primes between 1 and 10 are all the primes between 1 and 100. I think you are confusing primality with factoring. For all composite numbers there must exist a factor between 2 and the sqrt of the number.

"We can't do nothing and think someone else will make it right."

-Kyoto Now, Bad Religion

662The first one is actually 6, not 15. And no, i don't divide that out since that isn't a power of any integer.

: Anyways, if going by powers was faster...

It [b]is[/b] faster. Since you're opposing to that i'll have to assume that you didn't follow my advice about testing it.

Your deduction about removing multiples is correct but you missed to proof that it's the fastest one (which it isn't). You seem to have assumed that.

I'll be happy to explain to you (well, everybody reading this thread, actually) what the advance with power-method is compared to multiple-method. But first i'd like you to "see" it for yourself. Write the code and test it, it only takes ten minutes.

Kind Regards

Konrad

----------------------------

(+46/0) 708-70 73 92

chamster@home.se

http://konrads.webbsida.com