us fr nl

VBA : Project Euler: Project Euler - Problem #2   nl

Options: Save as PDF | Save attached file | Toggle line numbers


Type: sub
Added By: Rembo
Short Description:
From their website: 

"What is Project Euler?

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context."

After reading some of the solutions discussed in Dick's blog I thought it would be fun to post some solutions on this website as well. Rather than copying and pasting solutions of other people I will post my own solution. They may not be the most efficient solutions but they do the job. The challenge is of course to find your own solution, although the use of the internet is encouraged. Please see the Project Euler website for a full explanation.

If you find this challenging I encourage you to sign up (free) and see how far you get with programming your VBA solution.
The problem description is included with the code.
Added: Feb 5 2009 at 12:02 PM
Related URLs


Run the routine, the answer is printed in the debugging screen.


Formatted | Unformatted
  1. Sub Euler_0002()
  2. 'Each new term in the Fibonacci sequence is generated by adding the previous two terms.
  3. 'By starting with 1 and 2, the first 10 terms will be:
  4. '
  5. '1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  6. '
  7. 'Find the sum of all the even-valued terms in the sequence which do not exceed four million.
  9. Dim i1 As Long, i2 As Long, iTemp As Long, iTotal As Long
  10. i1 = 1
  11. i2 = 2
  12. iTotal = 2
  13. While i2 <= 4000000
  14. iTemp = i1 + i2
  15. i1 = i2
  16. i2 = iTemp
  17. If i2 Mod 2 = 0 Then
  18. iTotal = iTotal + i2
  19. End If
  20. Wend
  21. Debug.Print iTotal
  22. End Sub

User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website