Solved

non-Fibonacci number in Java

Posted on 2011-09-16
6
443 Views
Last Modified: 2012-05-12

My homework assignment is to add a nest loop to print the first twenty five (25) non – Fibonacci  numbers.  I don't want anyone to do my homework, but I stuck on how to get the non-Fibonacci numbers?  My "for loop" is not working?  Suggestions are welcome.  I'm a beginner at Java and EE so please let me know if my question is okay.  How many points should I give for a question like this?


Walter


public class Fibonum {

      public static void main(String[] args) {
      
                int first = 0, second = 1, newnum;
                int count = 2;

                
              //System.out.printf("%7d",first);
               //System.out.printf("%7d",second);
                
               while (count < 25){
               newnum = first + second;
       // System.out.printf("%7d",newnum);
          first = second;
          second = newnum;
                      count++;
         //       if (count % 5 == 0)
         //             System.out.println("   ");
                
                for(int nonFib = first + 1; nonFib < second; nonFib++){
                      System.out.println(nonFib + " ");
       
      
                      
                }          
               }
      }
}
0
Comment
Question by:WalterAPO
6 Comments
 
LVL 4

Accepted Solution

by:
reijnemans earned 42 total points
ID: 36548716
Hi,

Is it mandatory to do it with a nested loop it's possible to do it with just 1 for loop:

	public static void main(String[] args) {

		int first = 0; 
		int second = 1;

		int i = 0;
		int j = 0;
		while (i < 25) {
			if (j == first + second) {
				first = second;
				second = j;
			} else if (j != first || j == second) {
				i++;
				System.out.println(j);
			}
			j++;
			
		}
	}

Open in new window

0
 
LVL 4

Assisted Solution

by:stachenov
stachenov earned 42 total points
ID: 36548752
Your code looks almost fine to me, but it finds much more than 25 numbers, since you're incrementing count outside of the nested loop. I'd change the following:

1) Initialize count to 0;
2) Move "count++" into the nested loop;
3) Change the nested loop's condition to "nonFib < second && count < 25" so you find exactly 25 numbers.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36550205
Walter, my guess is that class is just starting.  What week are you in?  (This gives me an idea of how much you could have learned.)  I don't know how many points you should give but I think you'd have been better off posting this to "New to Java." because you are.  Still, for the type of help I'll offer, I think 500 points is good because someone like me will want you to learn (and thus I'll try to teach).  Teaching ain't easy.

Since I don't know your level, I'll tell you how I'd approach the problem. I might end up using terms you don't know yet.

I would create a class that would create a class for Fibonacci numbers.  It would have two fields, currentNumber and previousNumber.  The constructor (at least the one without arguments) would set currentNumber to 0 and previousNumber to 1.

I'd add accessors for both fields - getCurrentNumber() and getPreviousNumber().  Another method would be both a mutator and an accessor would be something like findNextNumber().  It would copy the previous to a temporary variable, copy previous to current and then add the new previous to the temporary variable to get the new current.  I'd have it return the new current.

Now you have a Fibonacci number generator.  I'd test that first.

My idea is you find the non Fibonacci numbers by knowing that they are the ones that aren't Fibonacci numbers.  There are multiple ways you can do this.  I'd modify the Fibonacci class and I can help you with that *after* you create the class to generate Fibonacci numbers.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 41 total points
ID: 36553219
I think the logic would be most starightforward if after you define in the beginning

int previousFib = 0;
int nextFib = 1;
int countNonfib = 0;

you make
outer loop as if infinite using while(true)
 (no reason, by the way, to be afraid of while(true) method,
if you provide clear condition for breaking out, like you would do below)

So you do:

while(true) {
//here insiode you make a "for"  loop
//starting with previousFib+1 and less than nextFib - more or less  like you have in you code
// and inside this loop you print running index of the loop - your nonFib numbers - and also each time increment countNonFib++
// and immediately after incrementing check - if countNonfib equals 25 you immediately exit the program with System.exit(0)

//after you close the for loop (if you didn't jump out of it, because of the System.exit(0))
//you store the value of nextFib  in temporary variable, say  int temp=nextFib;
// then assign new nextFib as the sum of  nextFib  and previousFib
// and then assign the vlaue of old nextFib (stored in temp)  to new previousFib

//and here your while(true) loop ends - so you go through the next step - again printing nonFib numbers between the next pair of Fibs
}


I tried this method an got this result, which I believe is correct
4,6,7,9,10,11,12,14,15,16,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32

Open in new window

0
 

Author Closing Comment

by:WalterAPO
ID: 36553398
Thank you for your help.  With your help I figured out my homework.  I still have 6 weeks of class left so I will be asking more questions.  I tried to increase the point, but I could get around the "You may not award more than 125 points." error.  Again, thank you.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36554209
In closing, I still suggest you try to think more in an OOP orientation than a procedural programming orientation.  My guess is you are left hemisphere dominant in your brain.  Most programmers are (such as myself).  I did a research paper on this for a college class a while back and the only negative comment was that it was a bit too brief.  Still got the A.  (I might know what I'm talking about.)

Some of the papers I read discussed the danger of left hemisphere dominant people, under the pressure to finish a project (at work), would revert to procedural methods instead of OOP methods.  While this would get the project done, sometimes they were "busted" by supervisors.

My strong suggestion is to really practice using classes, mutators, accessors, encapsulation, inheritance, etc.

I'm a person that has hired and fired programmers.  I'd notice.  Someone else might too.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 90
how to add new optional parameter to JSP 1 40
table example 4 29
tomcat startup error 5 59
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question