Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


rajnish
primrfibo
1 Answer(s)      5 years and 4 months ago
Posted in : Java Beginners



Advertisement
View Answers

September 7, 2009 at 4:48 PM


Hi Friend,

Try the following code:

import java.util.*;

public class PrimeAndFibbonacci {
int no;
private ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> generateSeries(int num) {
int f1, f2 = 0, f3 = 1;
for (int i = 1; i <= num; i++) {
list.add(f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
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>();
PrimeAndFibbonacci pf=new PrimeAndFibbonacci();
palindromes=pf.generateSeries(500);
Iterator<Integer> iter=palindromes.iterator();
while (iter.hasNext()){
int reqNo=iter.next();
if (isPrime(reqNo))
System.out.println(reqNo);
}
}
}

Thanks


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com