Project Euler: Problem 2

Challenge:

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, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Programming Language:

LISP

Solution:

(defun fib-even-sum (fl)
  (if (<= (+ (car (reverse fl)) (cadr (reverse fl))) 4000000)
      (progn
	(push (+ (car (reverse fl)) (cadr (reverse fl))) (cdr (last fl)))
	(fib-even-sum fl)
	)
      )
  (apply #'+ (remove-if-not 'evenp fl))
  )

Overview:

Coming soon…


Programming Language:

x86 Assembly

Solution:

; Receive n input. Print nth Fibonacci number.

format PE console
entry start

include 'win32a.inc'

; ============================================

section '.text' code readable executable

start:
	; The program begins here:

	call 	read_hex
	mov	ecx, eax	; setup our counter, ecx.
	dec	ecx		; offset ecx counter.
	mov	ebx, 0		; set ebx to 0. First Fib. num.
	mov	edx, 1		; set edx to 1. Second Fib. num.
	mov	esi, 1		; set esi to 1.
	cmp	eax, 0		; if initial input is above 0, begin. Otherwise, exit. 
	ja	fib		
	jmp	exit

fib:
	add	ebx, edx	; Add to first terms (0 + 1)
	mov	edx, esi	; Put previous term into edx
	mov	esi, ebx	; Put previous sum into esi
	dec	ecx

	;mov	eax, esi
	;call	print_eax

	cmp	ecx, 0
	jg	fib		; Jump if GREATER to avoid failing comparing to FFFF... 
				; when ecx wraps back around below 0 after last dec.
				; jg = signed. ja = unsigned.
	mov	eax, esi
	call	print_eax

exit:
	; Exit the process:
	push	0
	call	[ExitProcess]

include 'training.inc'

Overview:

Coming soon…