Results 1 to 13 of 13
- 04-27-2010, 04:30 PM #1
Am I using correct programming principles?
Hello!
I'm currently working on the first few problems from projecteuler.net, so that I can learn Java 'by doing' as well as reading.
Problem 2 is:
My code:Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
In case you didn't understand my variable naming, fibminus2 + fibminus1 will give you the next number in the sequence (newno).Java Code:public class ProblemSet { public static void main(String[] args) { int fibminus2 = 1; int fibminus1 = 2; int newno = 0; int total = 2; while(newno < 4000000){ newno = fibminus1+fibminus2; if(newno % 2 == 0){ total+=newno; } fibminus2 = fibminus1; fibminus1 = newno; } System.out.println(total); } }
I've been able to solve this problem, but the thing I would like to get feedback on is whether I wrote this in a 'correct' and 'efficient' way. I know this request is a little vague, and for that I'm sorry. I want to learn good programming principles from the very start. ;)
There is a thread on the Project Euler website where people can share their solutions. There are quite a few Java ones, but I can't really tell which ones use good principles and which one use bad/inefficient ones.
One question I have in particular is whether I should be using the while loop, like I did in my code, or whether I should be using the for loop (like this: for(int i = 0, i < 4000000, i++))
I'd very much appreciate help in any form!
Beefy
- 04-27-2010, 04:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
You're doing fine; there are (almost) no principles when it comes to a simple, single algorithm; the code is efficient as it is and changing the while loop to a for loop is just a matter of preferences. Your suggestion doesn't work because you don't have to loop 4000000 times, i.e. you have to quit your loop when the last fibonacci number is larger than (or equal) to 4000000.
kind regards,
Jos
- 04-27-2010, 06:36 PM #3
Thanks for your reply Jos!
I was wondering about whether I was going about it in the correct way, so that when I move on to larger and more complex problems, I'll be doing it the right way ;)
I guess I'll ask you guys again when I have a large problem that takes more processing time to complete ;)
Beefy
- 04-27-2010, 06:52 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
A bit of a tip (more of an exercise really): can you implement the same using a separate method that can deliver the answer? Your main( ... ) method should look like this:
The solveProblem() should do the hard work (which you already have done).Java Code:public static void main(String[] args) { System.out.println("The answer is: "+solveProblem()); }
kind regards,
Jos
- 04-27-2010, 08:18 PM #5
I think I got it:
Anything wrong with this code?Java Code:public class ProblemSet { public static void main(String[] args) { System.out.println(solveProblem()); } public static int solveProblem(){ int fibminus2 = 1; int fibminus1 = 2; int newno = 0; int total = 2; while(newno < 4000001){ newno = fibminus1+fibminus2; if(newno % 2 == 0){ total+=newno; } fibminus2 = fibminus1; fibminus1 = newno; } return total; } }
At first I defined the method solveProblem() without making it "static", but I got a error in Eclipse which added the "static" for me. What does that change?
- 04-27-2010, 08:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Well done. If you don't make that method static it belongs to an object (of the class where the method is defined). You don't have an object (instantiation) of that class so you can't call that method.
Another (small) nitpick: why did you make that method 'public'? Do you want to be able to call that method from somewhere else? Think about it; if you just want to call it from within that class (e.g. from your main( ... ) method) make that solveProblem( ... ) method private.
kind regards,
Jos
- 04-27-2010, 08:39 PM #7
Thanks Jos,
Yes, the method should be private. I learnt that yesterday, so I should know that :p
About the "static" method, I still don't really understand it yet, but I think I have to learn more about concepts like 'Objects', 'Classes', 'Methods' etc before I do. I'm heading back to the first few Stanford Opencourseware Java lectures so that I can force all of the concepts into my brain ;)
- 04-27-2010, 09:06 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
A class is a blueprint for an object (an instantiation) of that class; it tells what you (or anything else) can 'say' to that object; that 'saying things' are the methods that belong to the object. If I want to say giveMeYourWallet( ... ) I need something from the class Human to say it to. Object Oriented Programming (OOP) is all about objects that 'say' things to each other in order to get the job done; that's basically it and it nicely divides the notion of 'who does what' and 'why'. It doesn't just organize the data that is needed to get something done but it also organizes that 'whats' that have to get the job done.
kind regards,
Jos
- 04-28-2010, 10:26 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,471
- Rep Power
- 16
Following on from that, this is why you might encounter the concept of coding to interfaces. That is, you essentially define what something does by initially defining the interface. Only then do you write the actual class (ie the how it does something).
Though this might be little early on to actually be doing this (it's not so effective on small exercises)...worth bearing in mind, though.
- 04-28-2010, 11:37 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The Stanford courses are great, but here's another video tutorial that does a great job of giving you a running start.
Eclipse and Java for Total Beginners
It's geared around Eclipse, but the Java coverage is great too, and it also covers test-driven development. It moves quickly and covers quite a bit in about three hours of total video time, in bite-sized chunks.
-Gary-
- 04-28-2010, 11:42 AM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
By the way, to help get static methods clear in your head, play around with Math.random() and the Random class. Math.random() is a good example of a static method, while the Random class does the same job the more object-oriented way.
-Gary-
- 04-28-2010, 05:33 PM #12
Thanks guys for your help! :)
- 04-28-2010, 07:19 PM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
By the way, we were just toying around with a recursive method for fibbonnaci sequence at my class in uni. Since the definition of a number in the sequence is based on other numbers makes this problem inherently recursive:
Now, this is a pretty simple method, easy to understand, but it's slow as heck. The main problem is this: if you want to calculate the 5th element, first you have to calculate the 4th and 3rd element. Then you calculate the 4th again, the 3rd again and then the 2nd. A lot of calculations that were already done. A solution is, the try and memorize the results of calculations that you have already done. Try this on for size, and try to measure the running time for the normal method and the improved one for a large n value.Java Code:int fib(int n) { if(n <= 1) return n; return fib(n-1)+fib(n-2); }
Java Code:int fibMemo(int n) { Integer[] memo = new Integer[n+1]; return fibMemo(n, memo); } int fibMemo(int n, Integer[] memo) { if(memo[n] == null) {//we haven't calculated this yet if(n <= 1) memo[n] = n; //base condition else memo[n] = fibMemo(n-1, memo)+fibMemo(n-2, memo); } return memo[n]; //if the first if returns false, no calculations are made, because we //already calculated this number }Last edited by m00nchile; 04-28-2010 at 08:01 PM.
Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Are my comments correct?
By twiggy62 in forum New To JavaReplies: 2Last Post: 02-08-2010, 05:34 AM -
Getting correct output
By WarmRegards in forum New To JavaReplies: 9Last Post: 11-01-2009, 04:41 PM -
Is this the correct Output?
By Teny in forum New To JavaReplies: 17Last Post: 04-13-2009, 12:52 PM -
Is my Pseudocode correct?
By Clemenza1983 in forum New To JavaReplies: 0Last Post: 01-29-2008, 04:07 AM -
To correct forum
By Jman in forum IntroductionsReplies: 3Last Post: 01-18-2008, 02:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks