Created
January 7, 2019 17:04
-
-
Save feehe21/d5322dfbbbc923c24c611bb1b2abf090 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class sortPrimes | |
{ | |
public sortPrimes(){ | |
int[] primeSet = new int[10]; | |
primes(primeSet); | |
for(int i = 0; i < 10; i++){ | |
System.out.print(primeSet[i] + ", "); | |
} | |
sort(primeSet); | |
System.out.println(""); | |
for(int i = 0; i < 10; i++){ | |
System.out.print(primeSet[i] + ", "); | |
} | |
} | |
public void sort(int[] a){ | |
int lowest; | |
int save; | |
for(int i = 0; i < a.length - 1; i++){ | |
lowest = i; | |
for(int e = i + 1; e < a.length; e++){ | |
if(a[e] < a[lowest]){ | |
lowest = e; | |
} | |
} | |
save = a[i]; | |
a[i] = a[lowest]; | |
a[lowest] = save; | |
} | |
} | |
public void primes(int[] a){ | |
int created = 0; | |
int randomNum; | |
while(created < 10){ | |
randomNum = (int)(Math.random() * ((1000 - 0) )) + 0; | |
if(isPrime(randomNum)){ | |
a[created] = randomNum; | |
created++; | |
} | |
} | |
} | |
public boolean isPrime(int y) | |
{ | |
int compare = 2; | |
int remainder = 0; | |
boolean prime = false; | |
while(compare < y){ | |
remainder = y%compare; | |
if(remainder == 0){ | |
compare = y; | |
}else if (compare == (y-1)){ | |
prime = true; | |
} | |
compare ++; | |
} | |
return prime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment