Friday, 31 May 2013

CIRCULAR PRIMES



Circular primes are prime numbers which upon cyclic rotation also give prime numbers. For example: 197 is  prime number and its cyclic rotations 719 and 917 are also prime . Therefore 197 is a circular prime. It is to be noted that cyclic rotations and permutations are different concepts. Cyclic rotations are a subset of permutations of a given number.                                                                                                                                                                                                                                    
                                                              
There are 13 circular primes below 100:2 ,3 ,5 ,7 ,11 ,13 ,17 ,31 ,37 ,71 ,73 ,79 ,97.
Here is an interesting program to find the circular prime numbers between a given range of numbers input by the user. First of all, we’ll check whether the number is prime or not. If the number is prime then its cyclic rotations are further checked.
The circular primes with at least 2 digits can consist of combinations of only 1,3,7 or 9 as the numbers consisting of digits 2,4,6,8,0 or 5 will be either divisible by 2 or 5 and hence are not prime. Therefore the code can be shortened by checking that if any digit of the number is a multiple of 2 or 5 then the number can be skipped. Since 2 and 5 are prime ,if they are included in the range, they can be separately checked.  

Source code # 1 (in C++)
/*****************************************************************************/

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
                clrscr();
                int c=0,r,flag,d;
                unsigned long num,num1,num2,div,i,j,n1,n2,nn;
                cout<<"\n Enter the starting number of the range:";
                cin>>n1;
                cout<<"\n Enter the ending number of the range:";
                cin>>n2;
                for(i=n1;i<=n2;i++)
                {
                                if(i==1)
                                goto a;
                                if(i==2 || i==5)
                                goto b;
                                num2=i;
                                while(num2)  //if number consists of a digit that is multiple of 2 or 5 
                                                                              //then no need to check it.
                                {
                                                d=num2%10;
                                                if(d==5 || d%2==0)
                                                goto a;
                                                num2=num2/10;
                                }
                                num=i;
                                for(j=2;j<=sqrt(num);j++) //to check whether number is prime or not.
                                {
                                                if(num%j==0)
                                                goto a;
                                }
                                flag=0;
                                while(num>9)      //to count the number of digits in the number.
                                {
                                                num=num/10;
                                                flag++;
                                }
                                div=pow(10,flag);
                                num1=i;
                                while(flag) //here cyclic rotations of the number are formed and
                                                 //checked whether  they are prime or not.
                                {
                                                r=num1%10;
                                                nn=div*r+(num1/10);
                                                for(j=2;j<=sqrt(nn);j++)
                                                {
                                                                if(nn%j==0)
                                                                goto a;
                                                }
                                                num1=nn;
                                                flag=flag-1;
                                }
                                b:
                                c=c+1; // to count the number of circular primes.
                                cout<<"\t"<<i;
                                a:
                }
                cout<<"\n the number of circular primes between "
                <<n1<<" and "<<n2<<" are:"<<c;
                getch();
}
/*****************************************************************************/

Source code # 2 (in C) 

/*******************************************************************************/
#include<stdio.h>
#include<math.h>
#include<conio.h>

int isprime(long);
int main()
{
long int upper_limit,lower_limit,i,j,copy_i,new_num;
int ccounter=0,counter=0;
/*counter is used for counting the
total number of digits of any number

ccounter is used for counting
that how many numbers
that are circular permutations of
original number are primes
*/
clrscr();
printf("ENTER THE LOWER LIMIT : ");
scanf("%ld",&upper_limit);
printf("ENTER THE UPPERLIMIT : ");
scanf("%ld",&lower_limit);

for(i=upper_limit;i<=lower_limit;i++)
{
counter=0;ccounter=0;

copy_i=i;
while(copy_i)
{
counter++;
copy_i/=10;
}

copy_i=i;
if(isprime(i))
{
for(j=0;j<counter;j++)
{
new_num=copy_i%10;
copy_i/=10;
new_num*=(long)pow(10,(counter-1));
new_num+=copy_i;


if(isprime(new_num))
ccounter++;

copy_i=new_num;
}
if(ccounter==counter)
{
printf("%ld\t",i);
}
}
}
printf("\n\nDONE!!!");
getch();
return 1;
}

int isprime(long num)
{
long j;

for(j=2;j<=(num/2);j++)
{
if(num%j==0)
{
return 0;
}
}
return 1;
}
/******************************************************************************/
Output :-

Wednesday, 29 May 2013

java.lang.Math FUNCTIONS - A SHORT DESCRIPTION OF MOST OF THE MATHEMATICAL FUNCTIONS IN JAVA

Here are most of the methods that that are included in the Math class of java.lang package. I have mentioned most of the methods with prototype, description related to returning value of the method, and some examples for each method along with their output.



1-17 are the Rounding functions
18-21 are Exponential functions
22-28 are the Transcendental functions
29-32 are some Miscellaneous math functions.

S.NO.
METHOD PROTOTYPE
DESCRIPTION
EXAMPLE
OUTPUT
1.
static int abs(int arg)
Returns absolute value of arg
System.out.println(Math.abs(-5));
5
2.
static long abs(long arg)
Returns absolute value of arg
System.out.println(Math.abs(-256));
256
3.
Static float abs(float arg)
Returns absolute value of arg
System.out.println(Math.abs(-5.36f));
5.36
4.
static double abs(double arg)
Returns absolute value of arg
System.out.println(Math.abs(-5.31555));
5.3155





5.
static double ceil(double arg)
Returns the smallest integer greater than or equal to arg
System.out.println(Math.ceil(7.5));
8



System.out.println(Math.ceil(-7.8));
-7





6.
static double floor(double arg)
Returns the greatest integer less than or equal to arg
System.out.println(Math.floor(7.5));
7



System.out.println(Math.floor(-7.5));
-8





7.
static int max(int x,int y)
Returns the maximum of x and y
System.out.println(Math.max(3,5));
5
8.
Static long max(long x,long y)
Returns the maximum of x and y
System.out.println(Math.max(312546,598545));
598545
9.
static float max(float x,float y)
Returns the maximum of x and y
System.out.println(Math.max(3.2,5.6));
5.6
10.
static double max(double x,double y)
Returns the maximum of x and y
System.out.println(Math.max(3.1254,3.2145));
3.2145





11.
static int min(int x,int y)
Returns the minimum  of x and y
System.out.println(Math.min (3,5));
3
12.
Static long min(long x,long y)
Returns the minimum of x and y
System.out.println(Math.min (312546,598545));
312546
13.
static float min(float x,float y)
Returns the minimum of x and y
System.out.println(Math.min (3.2,5.6));
3.2
14.
static double min(double x,double y)
Returns the minimum of x and y
System.out.println(Math.min (3.1254,3.2145));
3.1254





15.
static double rint(double arg)
Returns an integer nearest to arg
System.out.println(Math.rint(2.3));
2



System.out.println(Math.rint(2.5));
2



System.out.println(Math.rint(2.8));
3



System.out.println(Math.rint(2.500001));
3





16.
static int round(float arg)
Return arg rounded up to nearest integer
System.out.println(Math.round(2.2));
2



System.out.println(Math.round(2.8));
3



System.out.println(Math.round(2.5));
3



System.out.println(Math.round(2.4999));
2





17.
static long round(double arg)
Return arg rounded up to nearest integer
System.out.println(Math.round(2.2));
2



System.out.println(Math.round(2.8));
3



System.out.println(Math.round(2.5));
3



System.out.println(Math.round(2.4999));
2





18.
static double exp(double arg)
Returns e to the power arg
System.out.println(Math.exp(5.3));
200.33680997479166





19.
static double log(double arg)
Returns the natural logarithm of arg
System.out.println(Math.log (5.3));
1.667706820558076





20.
static double pow(double x,double y)
Returns y raised to the power
System.out.println(Math.pow (5.3,2.3));
46.327034412240806






21.
static double sqrt(double arg)
Returns the square root of arg
System.out.println(Math.sqrt (5.3));
2.3021728866442674





22.
static double sin(double arg)
Returns the sine of angle (in radians) specified by arg
System.out.println(Math.sin (2.3));
0.7457052121767203





23.
static double cos(double arg)
Returns the cosine of angle (in radians) specified by arg
System.out.println(Math.cos (2.3));
-0.666276021279824





24.
static double tan(double arg)
Returns the tangent of angle (in radians) specified by arg
System.out.println(Math.tan (2.3));
-1.1192136417341325





25.
static double asin(double arg)
Returns the angle in radian whose sine is specified by agr
System.out.println(Math.asin (0.3));
0.3046926540153975



System.out.println(Math.asin (2.3));
NaN





26.
static double acos(double arg)
Returns the angle in radian whose cosine is specified by agr
System.out.println(Math.acos (0.3));
1.2661036727794992



System.out.println(Math.acos (2.3));
NaN





27.
static double atan(double arg)
Returns the angle in radian whose tangent is specified by agr
System.out.println(Math.atan (0.3));
0.2914567944778671





28.
static double atan2(double arg1,double arg2)
Returns the angle in radian whose tangent is arg1/arg2
System.out.println(Math.atan2 (5.3,2.3));
1.1613591172687538





29.
static double toRadians(double arg)
Returns angle in radians that was specified in degrees by user
System.out.println(Math.toRadians (60));
1.0471975511965976





30.
static double toDegrees(double arg)
Returns angle in degrees that was specified in radians by user
System.out.println(Math.toDegrees(1.325));
75.91690785483408





31.
static double random()
Returns a random number between 0 and 1
System.out.println(Math.random());
0.6442722773831705





32.
Static double IEEEremainder(double a,double b)
Returns  a-b*Math.rint((double)a/(double)b)
System.out.println(86,8)
-2.0



System.out.println(86.54,8)
-1.4599999999999937



System.out.println(90.54,8)
2.5400000000000063