美文网首页
Persistent Bugger

Persistent Bugger

作者: Magicach | 来源:发表于2017-12-25 22:31 被阅读0次

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

For example:

persistence(39) == 3 // because 39 = 27, 27 = 14, 1*4=4
// and 4 has only one digit

persistence(999) == 4 // because 999 = 729, 729 = 126,
// 126 = 12, and finally 1*2 = 2

persistence(4) == 0 // because 4 is already a one-digit number

Good Solution 1:

class Persist {
  public static int persistence(long n) {
    long m = 1, r = n;

    if (r / 10 == 0)
      return 0;

    for (r = n; r != 0; r /= 10)
      m *= r % 10;

    return persistence(m) + 1;
    
  }
}

Good Solution 2:

class Persist {
  public static int persistence(long n) {
    int times = 0;
    while (n >= 10) {
      n = Long.toString(n).chars().reduce(1, (r, i) -> r * (i - '0'));
      times++;
    }
    return times;
  }
}

相关文章

  • Persistent Bugger

    Write a function, persistence, that takes in a positive p...

  • react 基础之组件篇一

    React 引子 你问我为什么写博客? 因为阿拉斯加爱写bugger!!! 微信公众号:爱写bugger的阿拉斯加...

  • persistent!

    18/6/16 考试快乐!早早起床,来复习一下作文模板,句子词汇等等。早餐再晚也得吃,必须吃,有水的日子真棒,前几...

  • persistent

    人生若白驹过隙,忽然而已。最近的生活实在是太堕落了,要改变自己。希望以后自己可以坚持下面的几件事情,在本命年可以...

  • JAVA各种对象

    PO:持久对象 (persistent object),po(persistent object)就是在Objec...

  • 分享:JAVA各种对象

    PO:持久对象 (persistent object),po(persistent object)就是在Objec...

  • nvdimm

    nvdimm(non-volatile dimm)是persistent memory的一种。persistent...

  • nvdimm

    nvdimm(non-volatile dimm)是persistent memory的一种。persistent...

  • 六级核心词汇系列 | S6

    【每日词汇】 persistent [pərˈsɪstənt] a. 坚持不懈的 persistent effor...

  • android:persistent

    Case A Install test app with versionCode=1 and versionNam...

网友评论

      本文标题:Persistent Bugger

      本文链接:https://www.haomeiwen.com/subject/svvrgxtx.html