Thread: Prime numbers
View Single Post
  #2 (permalink)  
Old 01-31-2008, 10:47 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Spoiler
Hello gapper

Here's a spoiler:
Code:
public class Prime{ protected Vector<Integer> getFactors(int integer){ Vector<Integer> result = new Vector<Integer>(); for (int i = 1; i <= integer; i++) if (integer % i == 0) result.add(new Integer(i)); return result; } public Prime(int bound){ System.out.println("Prime numbers up to " + bound + ":"); for (int i = 2; i <= bound; i++){ if (getFactors(i).size() == 2) System.out.println(i); } } }
Use the constructor to run it
Code:
new Prime(20);
gives Output
Code:
Prime numbers up to 20: 2 3 5 7 11 13 17 19
This is a classical assignment. I could not resist.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote