Results 1 to 10 of 10
 08072010, 01:55 PM #1Member
 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 56 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; 08072010 at 02:28 PM. Reason: code tags added
 08072010, 02:29 PM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Do you want to fine a prime number within the Fibonacci series or wise verse?

Moving thread from Forum Lobby to the New to Java section.
 08072010, 03:21 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
IMHO it already helps quite a bit if you decompose your problem into smaller problems; as a first step a birdeye 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> }
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
 08072010, 05:04 PM #5Member
 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
 08072010, 05:10 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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
 08072010, 05:19 PM #7
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 }
Teaching myself java so that i can eventually join the industry! Started in June 2010
 08072010, 06:39 PM #8

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 crossposts. Many here refuse to help crossposters if they don't provide this notification as they value their time.
 08082010, 04:07 AM #10
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 25
Similar Threads

write a program to generate a series using runtime parameters
By satheeshtech in forum New To JavaReplies: 2Last Post: 02022010, 03:01 PM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 10:09 AM
Bookmarks