CIRCULAR PRIME NUMBERS
The circular prime number are the numbers those whose circulation formations (left to right ) are also primes.
Eg : 197 is circular prime
As 197 is prime
719 is prime ( left to right )
971 is prime ( left to right )
197 same
So as to solve this the
LOGIC IS:
Make a function that checks for if the number is prime or not .
Now if the number is prime,
Circulate the number to get its new formation.
Circulate the number to get its new formation.
For each formation check if t it is prime or not.
If the formation is prime,
Circulate again and make a new formation and check again
If the formation is prime,
Circulate again and make a new formation and check again
If formation isn't prime,
terminate.
terminate.
Keep on checking till you reach the same number again ( from which formations are made )
JAVA CODE
import java.util.Scanner; public class circular_prime { static boolean isPrime(long n){ for(long j=2;j<n;j++) if(n%j==0)return false; return true; } static int check(long n) { int j=1; while(n/10!=0) { j*=10;n/=10; } return j; } static boolean iscircular(long n) { int fact=check(n); long temp=0,temp2=n; while(n!=temp) { temp=temp2; temp=temp/10+(temp%10)*fact; temp2=temp; if(isPrime(temp)){} else return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub long n=0; Scanner sc=new Scanner(System.in); System.out.println("enter the value upto which circular primes are to be found"); n=sc.nextLong(); for(long i=2;i<=n;i++) if(isPrime(i)) if(iscircular(i)) System.out.println(i+" : Number is circular"); sc.close(); } }
No comments:
Post a Comment