Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 查找给定数字中每个数字的乘积_Scala_Numbers_Product_Digits - Fatal编程技术网

Scala 查找给定数字中每个数字的乘积

Scala 查找给定数字中每个数字的乘积,scala,numbers,product,digits,Scala,Numbers,Product,Digits,在scala中,我需要编写一个方法来查找给定数字中每个数字的乘积。我有以下片段 def productDigits(number: Int): Int = { def helper(current: Int, accumulator: Int): Int = { current match { case current if current < 10 => accumulator * current case _ => h

在scala中,我需要编写一个方法来查找给定数字中每个数字的乘积。我有以下片段

  def productDigits(number: Int): Int = {
    def helper(current: Int, accumulator: Int): Int = {
      current match {
        case current if current < 10 => accumulator * current
        case _ => helper(current / 10, accumulator * (current % 10))
      }
    }
    helper(number, 1)
  }
def productDigits(编号:Int):Int={
def辅助程序(当前:Int,累加器:Int):Int={
当前匹配{
如果电流<10=>蓄电池*电流,则外壳电流
案例=>helper(当前/10,累加器*(当前%10))
}
}
助手(编号1)
}

有更好的方法吗?

这里是OP的递归解决方案与Luis的一行程序的jmh基准测试

执行

sbt“jmh:run-i10-wi10-f2-t1工作台。So59652263”

在哪里

我们看到递归解决方案的吞吐量似乎比一行程序多7倍


number
String
而不是
Int

@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
class So59652263 {
  def _dexter2305(number: String): Int = {
    def helper(current: Int, accumulator: Int): Int = {
      current match {
        case current if current < 10 => accumulator * current
        case _ => helper(current / 10, accumulator * (current % 10))
      }
    }
    helper(number.toInt, 1)
  }

  def _luis(number: String): Int = number.map(_.asDigit).product

  val num: String = (math.random * 100000000).toInt.toString
  @Benchmark def dexter2305: Int = _dexter2305(num)
  @Benchmark def luis: Int = _luis(num)
}

然而,我们看到递归解决方案仍然具有更高的吞吐量,比
number
was
Int
时的吞吐量小了2.5倍

这里是OP的递归解决方案与Luis的一行程序的jmh基准

执行

sbt“jmh:run-i10-wi10-f2-t1工作台。So59652263”

在哪里

我们看到递归解决方案的吞吐量似乎比一行程序多7倍


number
String
而不是
Int

@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
class So59652263 {
  def _dexter2305(number: String): Int = {
    def helper(current: Int, accumulator: Int): Int = {
      current match {
        case current if current < 10 => accumulator * current
        case _ => helper(current / 10, accumulator * (current % 10))
      }
    }
    helper(number.toInt, 1)
  }

  def _luis(number: String): Int = number.map(_.asDigit).product

  val num: String = (math.random * 100000000).toInt.toString
  @Benchmark def dexter2305: Int = _dexter2305(num)
  @Benchmark def luis: Int = _luis(num)
}

然而,我们看到递归解决方案仍然具有更高的吞吐量,比
number
was
Int
时的吞吐量小了2.5倍

number.toString.map(u.toInt),product
如果您想要一行,或者如果您的原始编号首先被读取为字符串。但是,你的可能更有效(如果你已经有了一个Int)。@LuisMiguelMejíaSuárez一行代码中似乎有一个bug,它应该是
asDigit
,而不是
toInt
:)
number.toString.map(gulme.toInt),product
,如果你想要一行代码,或者如果你最初的数字是作为字符串读取的话。但是,你的效率可能更高(如果你已经有了Int)。@LuisMiguelMejíaSuárez一行中似乎有一个bug,它应该是
asDigit
而不是
toInt
:)如果
number
是一个字符串而不是一个Int,那么性能可能值得测试一下。@LuisMiguelMejíaSuárez你的意思是这样吗?是的,完全一样-如果它已经是一个Int,我肯定我的实现会比较慢,但不确定它是否已经是一个字符串。@LuisMiguelMejíaSuárez仍然领先,但差距较小,为2.5。哇,太棒了。在这种情况下,我想人们甚至可以通过逐字符迭代字符串来加快速度。如果
number
将是字符串而不是Int,那么性能可能值得测试。@LuisMiguelMejíaSuárez你的意思是这样的吗,与此完全一样——如果已经是Int,我肯定我的实现会慢一些,但不确定它是否已经是字符串。@LuisMiguelMejíaSuárez递归仍然领先,但差距较小,为2.5。哇,太棒了。在这种情况下,我想我们甚至可以通过逐字符迭代字符串来加快速度。
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
class So59652263 {
  def _dexter2305(number: String): Int = {
    def helper(current: Int, accumulator: Int): Int = {
      current match {
        case current if current < 10 => accumulator * current
        case _ => helper(current / 10, accumulator * (current % 10))
      }
    }
    helper(number.toInt, 1)
  }

  def _luis(number: String): Int = number.map(_.asDigit).product

  val num: String = (math.random * 100000000).toInt.toString
  @Benchmark def dexter2305: Int = _dexter2305(num)
  @Benchmark def luis: Int = _luis(num)
}
[info] Benchmark               Mode  Cnt         Score         Error  Units
[info] So59652263.dexter2305  thrpt   20  36237007.844 ± 1631349.975  ops/s
[info] So59652263.luis        thrpt   20  13975984.042 ± 1890083.941  ops/s