Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Math;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class SimpleNumbers {
- public static void main(String[] args) {
- long buttom = 2007;
- int amount = 15000000;
- NumberList numlist = new NumberList(buttom, amount);
- long start = System.currentTimeMillis();
- for(int i = 0; i < numlist.numAmount; i++) {
- NumberList.isSimple(numlist.numList.get(i));
- }
- long end = System.currentTimeMillis();
- System.out.println("Time taken: " + (end - start));
- }
- }
- class NumberList {
- public List<Long> numList;
- public long topBorder;
- public long buttomBorder;
- public long numAmount;
- public NumberList(long buttomBorder, int numAmount) {
- this.buttomBorder = buttomBorder;
- this.numAmount = numAmount;
- this.topBorder = this.buttomBorder + this.numAmount - 1;
- this.numList = new ArrayList<Long>();
- for(int i = 0; i < this.numAmount; i++) {
- this.numList.add(this.buttomBorder + i);
- }
- }
- public static boolean isSimple(long number) {
- for(int i = 2; i <= Math.sqrt(number); i++) {
- if(number%i==0) return false;
- }
- return true;
- }
- public void printArray() {
- for(int i = 0; i < numList.size(); i++) {
- long currItem = numList.get(i);
- if(this.isSimple(currItem)) {
- System.out.println(String.format("numList[%d] = %d is simple", i, currItem));
- } else {
- System.out.println(String.format("numList[%d] = %d is not simple", i, currItem));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement