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…

Project Euler: Problem 1

Challenge:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Programming Language:

LISP

Solution:

(defun 3n5 (num)
  (defparameter sum 0)
  (loop for x from 1 below num do
       (if (or (eq (mod x 3) 0) (eq (mod x 5) 0))
	   (setq sum (+ sum x))))
  (format t "Sum of all multiples of 3 or 5 below ~a is: ~a~%" num sum))

Overview:

Coming soon…