Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Generate prime numbers within fiboncacci series?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.
     */
    package javaapplication5;
     
    import java.util.Scanner;
     
    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 helloworld922; August 7th, 2010 at 08:35 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Generate prime numbers within fiboncacci series?

    its just not displaying me the prime numbe
    What does the program display? Can you post its output?

    Please use code tags when posting your code. Unformatted code is hard to read.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Generate prime numbers within fiboncacci series?

    Please surround your code with [highlight] tags. They make it much easier to read. Also, it's much easier to read your code if it's tabbed to the correct indentation.

    It is printing out the prime numbers, but it's printing them out to the next line. Also, you're re-printing the message every time you go through the prime check loop. I'd suggest moving this out of the for loop.

    Try changing your print statement to this:

    System.out.println(i + " is prime");

    Also, there are a few style issues with your code:

    1. In Java, you don't have to declare all your variables at the beginning of a method, and in fact it is poor practice to declare variables before you need them.

    2. You only use i as a loop variable, so put it's declaration inside the loop. You don't want anywhere else in your program to access that variable (actually, that variable won't even exist anywhere else).

    for (int i = 2; i <= num3/2; i++)

    3. Do not use integers as a boolean flag! There is a data type built-in to java for holding true/false called the boolean (surprise surprise). Use this variable if you need a true/false value.

    boolean isPrime = true;
    if (isPrime)
    {
        System.out.println("The number is prime");
    }
    else
    {
        System.out.println("The number isn't prime");
    }

    4. Make use of "helper methods". You can easily push out the job of calculating if a number is prime. This brings a level of abstraction to your code an helps you to easily organize it because once the isPrime method is written, you just need to call it anytime you want to check if a number is prime instead of copying/paste the actual code for the isPrime method. It also allows you to look only at checking if a specific section of the code, allowing you to easily tell where a potential problem in the code is.

    public static boolean isPrime(int number)
    {
        if (number < 2)
        {
            return false;
        }
     
        for(int i = 2; i <= number / 2; ++i)
        {
            if (number % i == 0)
            {
                return false;
            }
        }
        return true;
    }

    Then, in your loop all you have to do is this:

    while(num3 < n)
    {
        int old = num3;
        num3 = num1 + num2;
        num1 = num2;
        num2 = old;
        if (isPrime(num3))
        {
            System.out.println(num3 + " is prime.");
        }
    }

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Generate prime numbers within fiboncacci series?

    Hi,

    Thank You so much for the reply helloworld and norm. Yeah, i will surely post my output next time when i post a question.



    i had a few question on the program.


    1) why is RETURN TRUE written outside the for loop? can we write it as
          public static boolean isPrime(int number)
                 {
                      for(int i = 2; i <= number / 2; ++i)
                         {
                               if (number % i == 0)
                                    {
                                          return false;
                                  else
                                          return true;
                                    }
                        }
     
     
     
                   }

    2) i will surely use the boolean data type next time whenever i need a true/false value,but can you please modify the same program to get the appropriate answer


    3) public static boolean isPrime(int number) . is [public static] really required in this method,we can simply write boolean isprime(int number),can't we?


    4) can you please tell me what hilighted tag really is?


    Sorry if i have asked you too lame question.




    Kind Regards,
    Manish87
    Last edited by helloworld922; August 7th, 2010 at 10:39 AM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Generate prime numbers within fiboncacci series?

    Try running the code as if you were the computer:

    1. Pick an input number (say, 15)
    2. number > 15, continue with the method
    2. i = 2
    3. 15 % 2 = 1, so return true

    obviously 15 is not prime (3 * 5 = 15), but the program says it is.

    To try and grasp why it isn't on a higher level, putting the return true statement inside the for loop is essentially saying "is the number divisible by every number 2 to number/2? If it is, it isn't prime. If it is, it is prime". Because of the way the logic is laid out, it actually will only tell you if a number is even or odd in a rather round-about way.

    This is not the correct prime check algorithm. The prime check algorithm needs to see if there are any evenly divisible numbers between 2 to sqrt(number) inclusively (i.e. check divisibility by 2 and sqrt(number) if sqrt(number) is an integer). Only after it has checked all of these numbers can you tell the user that it is a prime number (at least with this algorithm, there are other algorithms that check primality differently).

    A static method is one that is associated with the class rather than a specific instanced object (basically you never use the "new" keyword so you can't have any instanced objects).

    Declaring that the method returns boolean is absolutely necessary in all cases (instance and static methods). Java does not provide a default return type, so you must tell the compiler what the return type of that method is going to be.

    In this case, the public can be left out (resulting in default access rights) because you're only using it in the same package (to be more specific, in the same class), but in general for a method like this you will want to provide public access.

    How to use the highlight tag:

    [highlight=Java]
    // code goes in here
    [/highlight]


    It's also in my signature block.

    I'll leave it for you to try and modify your code with the suggestions above (feel free to ask us if you get stuck on this part and post your code/what's going wrong).
    Last edited by helloworld922; August 7th, 2010 at 10:56 AM.

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Generate prime numbers within fiboncacci series?

    Cross posted
    Generate prime numbers within fiboncacci series? - Java Forums
    Generate prime numbers within fiboncacci series? - CodingForums.com

    db

Similar Threads

  1. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 12:31 PM
  2. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM
  3. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM