# Thread: Generate prime numbers within fiboncacci series?

1. Member
Join Date
Aug 2010
Posts
29
Rep Power
0

## Generate prime numbers within fiboncacci series?

Hey,

i am new to java programming,and i am working a simple program that generate prime numbers whithin the generated fibonacci series,i tryed a lot to track the error out,but its just not displaying me the prime number(though fibonnaci numbers are generated),hope this forum can help to track my error which is bugging me from the past 5-6 hours.(platform used is netbeans 6.9)

/* program to generate prime numbers within the fibonnaci numbers */

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

Java Code:
```package javaapplication5;
import java.lang.*;
import java.util.*;

public class Main
{
public static void main(String[] args)
{
int fib1,fib2,fib3,n=0,flag, int i;
int a[]=new int[20];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
fib1=0;
fib2=1;
System.out.println(fib1+ " " +fib2);
fib3=fib1+fib2;
while(fib3<n)

{
System.out.println(fib3);
fib3=fib1+fib2;
fib1=fib2;
fib2=fib3;

flag=1;

for(i=2;i<=fib3/2;i++)
{
if(fib3%i==0)
{
flag=0;

}
if(flag==1)
{
System.out.println("the prime numbers are");
System.out.println(i);
}

}

}

}
}```
Last edited by Eranga; 08-07-2010 at 02:28 PM. Reason: code tags added

2. Originally Posted by Manish87
Hey,

i am new to java programming,and i am working a simple program that generate prime numbers whithin the generated fibonacci series,i tryed a lot to track the error out,but its just not displaying me the prime number(though fibonnaci numbers are generated),hope this forum can help to track my error which is bugging me from the past 5-6 hours.(platform used is netbeans 6.9)
IMHO it already helps quite a bit if you decompose your problem into smaller problems; as a first step a bird-eye view would look like this:

Java Code:
```n= <maximum fibonacci number>
m= 0
while (m < n) {
if (isPrime(m))
print m
m= <next fibonacci number>
}```
It seems that you can generate fibonacci numbers alright but the prime finding issue warrants its own little method to keep the bird-eye view clean. Don't stick everything in a single loop in a single method, that makes my dizzy and blurs what you are actually trying to do.

Checking whether or not a number is a prime number is easy: check all numbers up to the square root of the number and see if they evenly divide your number; if one of them does your number is not a prime number, otherwise it is. There are more sophisticated methods for prime number checking in all sorts and sizes ...

kind regards,

Jos

3. Member
Join Date
Aug 2010
Posts
29
Rep Power
0
Hi,

thanks for the reply everyone. the program does generate the prime numbers but not the correct ones.

Eranga: i would like to generate only the prime numbers from the generated fibonnaci series(upto n)

josah:yes we can use a method for this,but is it possible to generate prime without using any method?

can you guys tell me where exactly the logical error has occured since i am working on a notepad(cant debug :( )

Kind Regards,
Manish

4. Originally Posted by Manish87
josah:yes we can use a method for this,but is it possible to generate prime without using any method?

can you guys tell me where exactly the logical error has occured since i am working on a notepad(cant debug :( )
Your program logic in the primality test logic is incorrect but you probably can't see it because your code is very 'Basic' style and its indentation sucks; that why I suggested to turn it in a separate method. But if you don't want that (due to some silly restriction?) you have to struggle through your own code ...

kind regards,

Jos

5. Originally Posted by Manish87
Hi,

thanks for the reply everyone. the program does generate the prime numbers but not the correct ones.

Eranga: i would like to generate only the prime numbers from the generated fibonnaci series(upto n)

josah:yes we can use a method for this,but is it possible to generate prime without using any method?

can you guys tell me where exactly the logical error has occured since i am working on a notepad(cant debug :( )

Kind Regards,
Manish
i had a quick google search an i didnt find any built in methods in the math class for finding prime numbers, so you will have to write your own, however theres many different examples on google.
Like josah said, its better to write the code for calculating a prime number in a method (code block) and then calling that method within the loop because it looks more presentable.

method would look something similiar to this

PHP Code:
```    private static boolean isPrime(int checkNo){
boolean isPrime = false;

//some code here to check if its prime or not, if it is prime then do
//isPrime = true; else just leave it as false

return isPrime; //then u return the results here
}```

6. Originally Posted by Darryl.Burke
Yikes. Original poster this suggests that you don't care if volunteers duplicate work that's been done and posted elsewhere, essentially wasting someone's time. Please don't do this but instead make it a habit to notify all threads of cross-posts. Many here refuse to help cross-posters if they don't provide this notification as they value their time.

7. Originally Posted by Fubarable
Yikes. Original poster this suggests that you don't care if volunteers duplicate work that's been done and posted elsewhere, essentially wasting someone's time. Please don't do this but instead make it a habit to notify all threads of cross-posts. Many here refuse to help cross-posters if they don't provide this notification as they value their time.
Well said Fubarable. I'm really hate this too.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•