Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created January 7, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/d5322dfbbbc923c24c611bb1b2abf090 to your computer and use it in GitHub Desktop.
Save feehe21/d5322dfbbbc923c24c611bb1b2abf090 to your computer and use it in GitHub Desktop.
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