Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created January 8, 2020 19:54
Show Gist options
  • Save raunaqsingh2020/cd434963f72a3d3c8e8123f823dc50da to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/cd434963f72a3d3c8e8123f823dc50da to your computer and use it in GitHub Desktop.
import java.util.*;
public class FunPrimes
{
public FunPrimes()
{
LinkedList list = new LinkedList();
Scanner sc = new Scanner(System.in);
boolean playing = true;
System.out.println("Welcome to the prime number checker!");
while(playing){
System.out.print("Enter a number to check if it's prime:");
int num = sc.nextInt();
if(checkPrime(num)){
System.out.println(num + " is a prime.");
list.insert(list, num);
}else{
System.out.println(num + " is not a prime.");
}
goldbach(num);
if(list.getSize(list)!=0){
System.out.print("your prime list: ");
list.printList(list);
System.out.println();
}
System.out.println("Keep Going? ('Y' or 'N')");
String cont = sc.next();
if(cont.equals("N"))
playing = false;
}
}
public boolean checkPrime(int y)
{
boolean prime = true;
if(y==0||y==1){
prime = false;
}else{
for(int i = 2; i < y; i++){
if(y%i==0)
prime = false;
}
}
return prime;
}
public void goldbach(int y){
//int[] primes = new int[2];
//primes[0] = 0;
//primes[1] = 0;
if(y > 0){
if(y%2==0 & y>2){
for(int i = 3; i <= y/2; i++){
if(checkPrime(i)&&checkPrime(y-i)){
System.out.println(y + " is the sum of the prime numbers " + i + " and " + (y-i));
}
}
}else{
System.out.println("Parameter is an odd number.");
}
}
//return primes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment