Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9068359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:10:22+00:00 2026-06-16T17:10:22+00:00

Spoiler alert, this is problem 5 of Project Euler. I am attempting to learn

  • 0

Spoiler alert, this is problem 5 of Project Euler.

I am attempting to learn Clojure and solved problem 5, but it is a couple orders of magnitude slower (1515 ms in Java versus 169932 ms in Clojure). I even tried using type hinting, unchecked math operations, and inlining functions all for naught.

Why is my Clojure code so much slower?

Clojure code:

(set! *unchecked-math* true)
(defn divides? [^long number ^long divisor] (zero? (mod number divisor)))

(defn has-all-divisors [divisors ^long num]
  (if (every? (fn [i] (divides? num i)) divisors) num false))

(time (prn (some (fn [^long i] (has-all-divisors (range 2 20) i)) (iterate inc 1))))

Java code:

public class Problem5 {
  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    int i = 1;
    while(!hasAllDivisors(i, 2, 20)) {
      i++;
    }
    long end = System.currentTimeMillis();
    System.out.println(i);
    System.out.println("Elapsed time " + (end - start));
  }

  public static boolean hasAllDivisors(int num, int startDivisor, int stopDivisor) {
    for(int divisor=startDivisor; divisor<=stopDivisor; divisor++) {
      if(!divides(num, divisor)) return false;
    }
    return true;
  }

  public static boolean divides(int num, int divisor) {
    return num % divisor == 0;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:10:23+00:00Added an answer on June 16, 2026 at 5:10 pm

    Some performance problems:

    • The (range 2 20) call is creating a new lazy list of numbers for every increment of i. This is expensive, and is causing lots of unnecessary GC.
    • You are doing a lot of boxing by passing through function calls. Even the (iterate inc 1) is doing boxing / unboxing at every increment.
    • You are traversing a sequence of divisors. This is slower than a straight iterative loop
    • mod is actually not a very well optimised function in Clojure at present. You are much better off using rem

    You can solve the first problem by using a let statement to define the range just once:

    (time (let [rng (range 2 20)]
      (prn (some (fn [^long i] (has-all-divisors rng i)) (iterate inc 1)))))
    => "Elapsed time: 48863.801522 msecs"
    

    You can solve the second problem with loop/recur:

    (time (let [rng (range 2 20)
               f (fn [^long i] (has-all-divisors rng i))]
           (prn (loop [i 1] 
                  (if (f i)
                    i
                    (recur (inc i)))))))
    => "Elapsed time: 32757.594957 msecs"
    

    You can solve the third problem by using an iterative loop over the possible divisors:

    (defn has-all-divisors [^long num]
      (loop [d (long 2)]
        (if (zero? (mod num d))
          (if (>= d 20) true (recur (inc d)))
          false)))
    
     (time (prn (loop [i (long 1)] (if (has-all-divisors i) i (recur (inc i))))))
     => "Elapsed time: 13369.525651 msecs"
    

    You can solve the final problem using rem

    (defn has-all-divisors [^long num]
      (loop [d (long 2)]
        (if (== 0 (rem num d))
          (if (>= d 20) true (recur (inc d)))
          false)))
    
     (time (prn (loop [i (long 1)] (if (has-all-divisors i) i (recur (inc i))))))
    => "Elapsed time: 2423.195407 msecs"
    

    As you can see, it is now competitive with the Java version.

    In general, you can usually make Clojure almost as fast as Java with a bit of effort. The main tricks are usually:

    • Avoid lazy functional features. They are nice, but add some overhead which can be problematic in low-level computation-intensive code.
    • Use primitive / unchecked maths
    • Use loop/recur rather than sequences
    • Ensure you are not doing any reflection on Java objects (i.e. (set! *warn-on-reflection* true) and eliminate all warnings you find)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Spoiler alert: this is related to Problem 14 from Project Euler. The following code
SPOILER ALERT: Don't look at this if you are trying to solve Project Euler's
I'm trying to change a spoiler but i have problem with javascript code This
There is another recent Project Euler question but I think this is a bit
I'm attempting to learn Spring MVC but have been spoiled by Grails. While I
This is a brute force attempt to solve the problem, but it is not
Spoiler alert: this is a stupid question ;) I have a Rails form where
I'm not a regex expert, but after a couple hours I've built this regex:
I've been wrestling with Project Euler Problem #16 in C# 2.0. The crux of
UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.