StackOverFlow同时计数数字

StackOverFlow同时计数数字

本文介绍了StackOverFlow同时计数数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c> 

我试图计算Clojure中一个数字中的数字位数,如下所示: $ c>(defn num-digits [n]
(if(= 0 n)
0
(inc(num n digits(/ n 10))))) (println(num-digits 93))

但是如果我替换为unchecked-divide至少93.但是这两种技术都不适用于:

  93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 

首先,我想知道如何在Clojure中执行C风格的划分。每当我做(/ x y)我得到一个比率,而不是一个整数。



其次,有一种方法API可以将这个数字转换成一个数字和调用计数的向量。



感谢,

Ajay G

解决方案

这是为什么您遇到问题:

  user> (采用10(迭代#(/%10)10923))

(10923 10923/10 10923/100 10923/1000 10923/10000 10923/100000 10923/1000000 10923/10000000 10923/100000000 10923 / 1000000000)

这是修复:

  user> (take 10(iterate#(quot percent 10)10923))

(10923 1092 109 10 1 0 0 0 0 0)

这是您要查找的表达式:

  user> (count(take-while#(not(zero?%))(iterate#(quot;%10)10923)))
5


这是作弊行为:

  (count(str 10923))
5

这是你试图写的函数(但小心,它将堆栈溢出为大数):

  user> (defn num-digits [n] 
(if(= 0 n)
0
(inc(num-digits(n 10)))))
$ b b#'user / num-digits
user> (数字10923)
5

但是, / p>

  user> (num-digits 93326215443944152681699238856266700490715968264861641468592963860214685929638862175999932299156089414639761565158894146397615651828625369792082722375825118521091686400008000000000000)

158

此版本的功能不吹堆栈:

  user> (defn num-digits-tail-recursion 
([n count]
(if(= 0 n)
count

([n](num-digits-tail-recursion n 0)))
#'user / num-digits-tail-recursion
user> (num-digits-tail-recursion 10923)
5

自己的方式。好问题!


I am trying to count the number of digits in a number in Clojure as follows: I get a StackOverflowError even for 2 digit numbers

(defn num-digits [n]
   (if (= 0 n)
   0
   (inc (num-digits (/ n 10)))))
(println (num-digits 93))

But if I replace / with unchecked-divide then it works for at least 93. But neither of the techniques works for:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

First, I would like to know how to perform division the C-style in Clojure. Whenever I do (/ x y) I get a Ratio and not a Integer. What is the way to do it?

Secondly, is there a way API to convert this Number into a vector of digits and call count on it.

Thanks,
Ajay G

解决方案

This is why you're having a problem:

user> (take 10 (iterate #(/ % 10) 10923))

(10923 10923/10 10923/100 10923/1000 10923/10000 10923/100000 10923/1000000 10923/10000000 10923/100000000 10923/1000000000)

This is the fix:

user> (take 10 (iterate #(quot % 10) 10923))

(10923 1092 109 10 1 0 0 0 0 0)

This is the expression you're looking for:

user> (count (take-while #(not (zero? %)) (iterate #(quot % 10) 10923)))
5

This is cheating:

user> (count (str 10923))
5

This is the function you were trying to write (but careful, it will stack overflow for large numbers):

user> (defn num-digits [n]
        (if (= 0 n)
          0
          (inc (num-digits (quot n 10)))))

#'user/num-digits
user> (num-digits 10923)
5

However, it is up to the challenge:

user> (num-digits 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000)

158

This version of that function will not blow stack:

user> (defn num-digits-tail-recursion
        ([n count]
           (if (= 0 n)
             count
             (recur (quot n 10) (inc count))))
        ([n] (num-digits-tail-recursion n 0)))
#'user/num-digits-tail-recursion
user> (num-digits-tail-recursion 10923)
5

All versions are interesting in their own way. Good question!

这篇关于StackOverFlow同时计数数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:47