Skip to main content
Code Review

Return to Question

added 6 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Project Euler #20, Improving My Solution solution in Clojure

Problem:

n! means n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1
For example, 10! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800,
and the sum of the digits in the number 10! is 3 +たす 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27.
Find the sum of the digits in the number 100!

\$n!\$ means \$n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1\$

For example, \10ドル! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800\$, and the sum of the digits in the number \10ドル!\$ is \3ドル + 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27\$.

Find the sum of the digits in the number \100ドル!\$.

My solution in Clojure:

(reduce + (map (fn[x](Integer. (str x))) (seq (str (apply *' (range 1 101))))))

WonderingQuestions:

  • Is there a way to avoid the *' in the factorial bit? (apply *' (range 1 101))
  • I converted the result of the factorial to a string, then to a sequence, and then mapped an Integer cast to a string cast. Surely there must be a way to simplify this?

Project Euler #20, Improving My Solution

Problem:

n! means n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1
For example, 10! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800,
and the sum of the digits in the number 10! is 3 +たす 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27.
Find the sum of the digits in the number 100!

My solution in Clojure:

(reduce + (map (fn[x](Integer. (str x))) (seq (str (apply *' (range 1 101))))))

Wondering:

  • Is there a way to avoid the *' in the factorial bit? (apply *' (range 1 101))
  • I converted the result of the factorial to a string, then to a sequence, and then mapped an Integer cast to a string cast. Surely there must be a way to simplify this?

Project Euler #20 solution in Clojure

Problem:

\$n!\$ means \$n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1\$

For example, \10ドル! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800\$, and the sum of the digits in the number \10ドル!\$ is \3ドル + 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27\$.

Find the sum of the digits in the number \100ドル!\$.

My solution in Clojure:

(reduce + (map (fn[x](Integer. (str x))) (seq (str (apply *' (range 1 101))))))

Questions:

  • Is there a way to avoid the *' in the factorial bit? (apply *' (range 1 101))
  • I converted the result of the factorial to a string, then to a sequence, and then mapped an Integer cast to a string cast. Surely there must be a way to simplify this?
Source Link

Project Euler #20, Improving My Solution

Problem:

n! means n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1
For example, 10! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800,
and the sum of the digits in the number 10! is 3 +たす 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27.
Find the sum of the digits in the number 100!

My solution in Clojure:

(reduce + (map (fn[x](Integer. (str x))) (seq (str (apply *' (range 1 101))))))

Wondering:

  • Is there a way to avoid the *' in the factorial bit? (apply *' (range 1 101))
  • I converted the result of the factorial to a string, then to a sequence, and then mapped an Integer cast to a string cast. Surely there must be a way to simplify this?
lang-clj

AltStyle によって変換されたページ (->オリジナル) /