Results 1 to 10 of 10
  1. #1
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Manish87 View Post
    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

  5. #5
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Manish87 View Post
    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

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Manish87 View Post
    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
        }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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.

Similar Threads

  1. Replies: 2
    Last Post: 02-02-2010, 03:01 PM
  2. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

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