Prime Numbers from range

Prime Numbers from range

how to find prime numbers in a given range which are palindromes??

View Answers

December 21, 2010 at 5:12 PM

Hi Friend,

Try the following code:

import java.util.*;

public class PrimeAndPalindrome {
   static final int MAXNUMBER=200;
   private int number;
   private ArrayList<Integer> list = new ArrayList<Integer>();
   public PrimeAndPalindrome(int maxNum){
   number=maxNum;
   }
 ArrayList<Integer> isPalindrome(){
      for (int i=10; i<=number; i++){
         Integer in=new Integer(i);
         String number=in.toString();
         int index1=0;
         int index2=number.length()-1;
         char x=number.charAt(index1);
         char y=number.charAt(index2);
         while (index2-index1>=0){
         if(x!=y)
         break;
         if (index2-index1==0 || index2-index1==1)
         list.add(i);
         index1++;
         index2--;
         x=number.charAt(index1);
         y=number.charAt(index2);
         }
      }
      return list;
   }
   static boolean isPrime(int number){
      boolean isPrime=false;
      int i=(int) Math.ceil(Math.sqrt(number));
      while (i>1){
         if ((number!=i) && (number%i==0)){
            isPrime=false;
            break;
         }
         else if (!isPrime)
         isPrime=true;
         --i;
      }
      return isPrime;
   }
   public static void main(String[] args) {
      ArrayList<Integer> palindromes=new ArrayList<Integer>();
      PrimeAndPalindrome pp=new PrimeAndPalindrome(MAXNUMBER);
      palindromes=pp.isPalindrome();
      Iterator<Integer> iter=palindromes.iterator();
      while (iter.hasNext()){
         int current=iter.next();
         if (isPrime(current))
            System.out.println(current);
      }
   }
}

Thanks


December 23, 2010 at 10:49 AM

thnks buddy..but can u clearify what that ArrayList exactly doing?

Ads









Related Tutorials/Questions & Answers:
Prime Numbers from range
Prime Numbers from range  how to find prime numbers in a given range which are palindromes??   Hi Friend, Try the following code: import java.util.*; public class PrimeAndPalindrome { static final int MAXNUMBER
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
Advertisements
finding the prime numbers
finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about... number is prime or not, follow the link and it will provide you in depth knowledge
ModuleNotFoundError: No module named 'Prime_Numbers'
ModuleNotFoundError: No module named 'Prime_Numbers'  Hi, My... 'Prime_Numbers' How to remove the ModuleNotFoundError: No module named 'Prime_Numbers' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Prime_Numbers'
ModuleNotFoundError: No module named 'Prime_Numbers'  Hi, My... 'Prime_Numbers' How to remove the ModuleNotFoundError: No module named 'Prime_Numbers' error? Thanks   Hi, In your python
Generating random numbers in a range with Java
Generating random numbers in a range with Java  Generating random numbers in a range with Java
Palindromic Prime Numbers
Palindromic Prime Numbers  Write a program that finds the first palindromic prime number above 100,000. Include the value of this prime in a comment at the start of the code
ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers'
ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers'  Hi...: No module named 'Factors-and-Prime-Numbers' How to remove the ModuleNotFoundError: No module named 'Factors-and-Prime-Numbers' error? Thanks  
Prime numbers in Java between 1 and 100
Prime numbers in Java between 1 and 100  Hi, How to display prime numbers in java between 1 and 100? Thanks   Hi, Prime numbers... which prints prime numbers between 1 and 100. Here is complete for printing prime
print the even numbers between the range 100-200
print the even numbers between the range 100-200  Q ?write an application that print out the even numbers between the range 100-200 ? 1- which aer... prints out the even numbers between the range of 100 and 200 and then determine
write an application that print out the even numbers between the range 100-200
write an application that print out the even numbers between the range 100-200   write an application that print out the even numbers between the range 100-200 ?   The given code accepts the number from 100 to 200
Program to display palindrome numbers between some range
Program to display palindrome numbers between some range  Hi!I want a java program to display palindrome numbers between 100 to 1000.can you please explain me the logic with an example   import java.util.*; public
Prime Number in Java
a Java program to get prime numbers between 1 to the desired number. To start... the bytes. In this example of writing java program to get prime numbers, we.... And another loop will start and divide it from 2 to less than those numbers. Moreover
Java find prime numbers without using break statement
Java find prime numbers without using break statement In this tutorial, you will learn how to find the prime numbers without using break statement. You all are aware of Prime Numbers, these are the numbers which are either divided
Prime Number program in Java
Prime Number program in Java will print the prime numbers between 1 to any... automatically prints Prime Number starting from 1 to 50. Example of Prime Number... + " "); } } } Output: Prime Number from 1 to 50 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43
Generate random numbers from 1 to 100
Generate random numbers from 1 to 100  1)A class Called: RandomNumberGenerator that generate random numbers from 1 to 100 2)A class Test that tests... an object from the Random NumberGenerator class to generate your input test data
read a positive real numbers from highest to lowest
read a positive real numbers from highest to lowest  write a java program that will read a sequence of 10 positive real nos. entered by the user and will print the same numbers in sorted order from lowest to highest using arrays
prime palindrome
prime palindrome  Hi I want to write a program that ask a user to input a number, the program then list all the prime numbers that are palindrome below the number entered. In pascal language Thanks
prime number question - Java Beginners
has to input the two numbers and it displays the prime numbers within the range...; System.out.println("Prime Numbers between "+num1+" and "+num2...prime number question  For the question located here: http
prime number
prime number  HI!I want a java program that accepts a number from user and displays whether it is a prime number or not using BufferedReader
Mysql Time Range
numbers. Understand with Example The Tutorial illustrate an example from 'Mysql Time Range'. To understand example we create a table 'timetable' with fields... Mysql Time Range      
prime number
prime number  To check a number entered by user is prime
prime number
prime number  write a java program for prime number
prime number
prime number  check prime number using if statement   Java Check Prime Number
prime number
prime number  check prime number using if statement   Java Check Prime Number
prime number
prime number  get one number and check the number prime or not using if statement   Java Check Prime Number
prime palendrome
prime palendrome  Hi I want to write a program that ask a user to input a number, the program then list all the prime number that are palindrome below the number entered. Thks
prime palindrome
prime palindrome  Hi I want to write a program in pascal that ask a user to input a number, the program then list all the prime number that are palindrome below the number entered. thks
prime number
prime number  To check a number entered by user is prime or not?   Java check prime number import java.util.*; class CheckPrimeNumber... is Prime!"); } else{ System.out.println("Number
prime number
prime number  to check wethere given number is prime or not?  ... is Prime!"); } else{ System.out.println("Number is not prime!"); } } } Thanks
prime number
prime number  could u pls help me out in finding whether a number is a prime number or not?   import java.util.*; class CheckPrimeNumber... is Prime!"); } else{ System.out.println("Number
date range in sql
date range in sql  How can I select a random date from a date range in SQL server
prime nos
prime nos  Instead of using break statement in finding the prime nos ,is there any other way ?? if it is there pls tell that also   import java.io.*; class FindPrimeWithoutBreak { public static void main(String
Prime number program in java
prime number from 1 to " + limit); for(int number = 2; number<=limit; number++){ //print prime numbers only...Prime number program in java In this example you will learn how to write
ModuleNotFoundError: No module named 'is-prime'
ModuleNotFoundError: No module named 'is-prime'  Hi, My Python... 'is-prime' How to remove the ModuleNotFoundError: No module named 'is-prime... to install padas library. You can install is-prime python with following
ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'prime' How to remove the ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'
ModuleNotFoundError: No module named 'prime'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'prime' How to remove the ModuleNotFoundError: No module named 'prime'
range of dates in mysql query
range of dates in mysql query  How to select the range of dates in mysql ..when i have to select from between & and of date function?   SELECT * FROM MAAccounts WHERE updatedOn BETWEEN ? AND ? ORDER
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
Rational Numbers
Rational Numbers   Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator
range of int in c
range of int in c  What is the range of int in c/c++ and Java programming language
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi, You function will take 2 numbers and the find the even numbers between
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
ModuleNotFoundError: No module named 'range'
ModuleNotFoundError: No module named 'range'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'range' How to remove the ModuleNotFoundError: No module named 'range'
Hexadecimal numbers multiplication
Hexadecimal numbers multiplication  Sir, I have to multiply 128 bit hexadecimal numbers. Do u have any logic for this?? The numbers are like ab7564fa342b5412c34d9e67ab341b58
Addition of two numbers
Addition of two numbers  addition of two numbers
Sum of first n numbers
; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum...Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class
Version of com.real-comp>prime dependency
List of Version of com.real-comp>prime dependency
ModuleNotFoundError: No module named 'numbers'
ModuleNotFoundError: No module named 'numbers'  Hi, My Python... 'numbers' How to remove the ModuleNotFoundError: No module named 'numbers... to install padas library. You can install numbers python with following command

Ads