// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{
public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}
System.out.println("For "+i+", the co-primes are:");
for(int x=1; x {
for(y=0; y {
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}
public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
Showing posts with label Prime number. Show all posts
Showing posts with label Prime number. Show all posts
Friday, February 10, 2012
Wednesday, February 8, 2012
java program To find the coprimes of a number
// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{
public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}
System.out.println("For "+i+", the co-primes are:");
for(int x=1; x{
for(y=0; y{
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}
public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
public class Coprimes
{
public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}
System.out.println("For "+i+", the co-primes are:");
for(int x=1; x{
for(y=0; y{
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}
public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
Saturday, February 6, 2010
Shortest Function to find if a number is prime
class prime
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
Subscribe to:
Posts (Atom)