Ir al contenido principal

Hi, I'm Mariano Guerra, below is my blog, if you want to learn more about me and what I do check a summary here: marianoguerra.github.io or find me on twitter @warianoguerra or Mastodon @marianoguerra@hachyderm.io

project euler - problema 3

problema 3 y observaciones

el operador de modulo en erlang es muy poco intuitivo.

ej

1> 2.0 rem 2.
** exception error: bad argument in an arithmetic expression
in operator rem/2
called as 2.0 rem 2

si salta una excepcion en un guard en erlang no te dice que fue malo solo falla el guard

y alguna cosa mas que no me acuerdo.

python

import math

def factors(value):
factors = []
factor = 2.0
new_value = value
val_sqrt = math.sqrt(value)

while factor < val_sqrt:
if new_value % factor == 0:
factors.append(factor)
#new_value /= factor

factor += 1

return factors

def calculate(value):
return factors(value)[-1]

def print_results():
print calculate(600851475143)

if __name__ == '__main__':
print_results()


erlang

-module(ej_003).
-compile(export_all).

factors(Factor, _Value, Limit, Accum) when Factor > Limit -> lists:reverse(Accum);
factors(Factor, Value, Limit, Accum) when Value rem Factor == 0 ->
factors(Factor + 1, trunc(Value/Factor), Limit, [Factor | Accum]);
factors(Factor, Value, Limit, Accum) ->
factors(Factor + 1, Value, Limit, Accum).

factors(Value) -> factors(2, Value, trunc(math:sqrt(Value)), []).

print_results() ->
Factors = factors(600851475143),
io:format("~w~n", [Factors]),
if
length(Factors) > 0 -> io:format("~w~n", ([lists:last(Factors)]));
true -> io:format("No factor~n")
end.



lisp

(defun factors-helper (value limit)
(loop for factor from 2 when (= (rem value factor) 0) collect factor while (< factor limit)))

(defun factors (value) (factors-helper value (truncate (sqrt value))))

(defun print-results () (print (last (factors 600851475143))))

(print-results)