Clojure Integer overflow using decrement function

I am currently looking at project euler issues using Clojure. The second question requires you to summarize the first 400,000 fibonation numbers. Unfortunately, my code gives me an Integer Overflow exception.

(defn even-fib-sum [n]
   (reduce + 
      (filter even? 
         (take n (map first (iterate (fn [[x y]] [y (+ x y)]) [0 1]))))))

The problem occurs when I call a function and pass the value 4000000

(even-fib-sum 4000000) -> throws exception

(even-fib-sum 40) -> 82790070 
+4
source share
1 answer

use +'instead +to get auto-update added to bigintegers

(reduce +' 
   (filter even? 
     (take n (map first (iterate (fn [[x y]] [y (+' x y)]) [0 1])))))

Cloujure longs . , , , , , , , +', *' -', ,

+7

Source: https://habr.com/ru/post/1569486/


All Articles