Created
March 27, 2015 10:19
-
-
Save MarksCode/516f0b4572d3a63a3794 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/**********************/ | |
/* Prime Numbers */ | |
/* by Ron Marks */ | |
/* */ | |
/* This program finds */ | |
/* out if an integer */ | |
/* typed in is prime. */ | |
/* */ | |
/* Limitation: */ | |
/* Up to ~250,000 */ | |
/**********************/ | |
import java.util.Scanner; | |
public class Prime { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
boolean p; | |
System.out.println("Type in a positive integer, 0 to exit:"); | |
while(true){ | |
int req = scan.nextInt(); | |
if(req == 0){ | |
System.out.println("Bye"); | |
break; | |
} | |
else{ | |
p = isPrime(req); | |
if(p){ | |
System.out.println(req + " isn't a prime!"); | |
} | |
else{ | |
System.out.println(req + " is prime!"); | |
} | |
} | |
} | |
} | |
public static boolean isPrime(int n){ | |
if(n == 1){ | |
return true; | |
} | |
int counter = 1; | |
double a = 0.0; | |
for(int i=2; i<=n/2; i++){ | |
for(int j = n - counter; j>1; j--){ | |
a = (i * j); | |
if(a == n){ | |
return true; | |
} | |
} | |
counter = 1; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment