Skip to content

Instantly share code, notes, and snippets.

@rahman99
Last active June 7, 2016 07:49
Show Gist options
  • Select an option

  • Save rahman99/dfcd611f75857fb803f1e9898fb6df54 to your computer and use it in GitHub Desktop.

Select an option

Save rahman99/dfcd611f75857fb803f1e9898fb6df54 to your computer and use it in GitHub Desktop.
Prime Number
private static boolean isPrime(int number){
if(number<=1)
return false;
for(int i=2;i<Math.sqrt(number);i++){
if(number % i ==0)
return false;
}
return true;
}
private static void primeN(int n){
int count=2, status=1, num=3;
if(n>=1)
System.out.print(2+" ");
while(count<=n){
for(int j=2;j<=Math.sqrt(num);j++){
if(num%j==0){
status = 0;
break;
}
}
if(status != 0){
System.out.print(num+" ");
count++;
}
status =1;
num++;
}
}
private static void prime(int beforeNumber){
String prime="";
int num=0;
for(int i=1;i<=beforeNumber;i++){
int count=0;
for(num =i; num>=1; num--) {
if(i%num==0)
count = count + 1;
}
if(count==2){
prime += i+" ";
}
}
System.out.print(prime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment