Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

    • Abs [绝对值]
    • AliquotSum [真因数和]
    • Area [面积]
    • ArithmeticGeometricMean [算术-几何平均数]
    • ArmstrongNumber [阿姆斯特朗数]
    • AverageMean [平均数]
    • AverageMedian [中位数]
    • BinaryExponentiation [二分求幂]
    • BisectionMethod [二分法]
    • BinaryConvert [二进制转换]
    • CheckKishnamurthyNumber [克里希纳穆尔西数]
      • 介绍
      • 实现
      • 参考
    • Coordinate
    • CoPrimeCheck
    • DecimalIsolate
    • DegreeToRadian
    • DecimalExpansion
  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Math 数学
jonsam
2022-10-17
目录

CheckKishnamurthyNumber [克里希纳穆尔西数]

# 介绍

克里希纳穆尔西数是一个数字,其数字的阶乘之和等于该数字本身。例如,145,每个数字的阶乘之和: 1!+ 4!+ 5! = 1 + 24 + 120 = 145 。

有趣的是,我们知道的克里希纳穆尔西数字正好有四个,即 1、2、145 和 40585。

# 实现

# JavaScript

/*
    Problem statement and Explanation : https://www.geeksforgeeks.org/check-if-a-number-is-a-krishnamurthy-number-or-not-2/

    krishnamurthy number is a number the sum of the all factorial of the all dights is equal to the number itself.
    145 => 1! + 4! + 5! = 1  + 24 + 120 = 145
*/

// factorial utility method.
const factorial = (n) => {
  let fact = 1
  while (n !== 0) {
    fact = fact * n
    n--
  }
  return fact
}

/**
 * krishnamurthy number is a number the sum of the factorial of the all dights is equal to the number itself.
 * @param {Number} number a number for checking is krishnamurthy number or not.
 * @returns return correspond boolean value, if the number is krishnamurthy number return `true` else return `false`.
 * @example 145 => 1! + 4! + 5! = 1  + 24 + 120 = 145
 */
const CheckKishnamurthyNumber = (number) => {
  // firstly, check that input is a number or not.
  if (typeof number !== 'number') {
    return new TypeError('Argument is not a number.')
  }
  // create a variable to store the sum of all digits factorial.
  let sumOfAllDigitFactorial = 0
  // convert the number to string for convenience.
  let newNumber = number
  // Extract number digits using the remainder method.
  while (newNumber > 0) {
    const lastDigit = newNumber % 10
    // calculate each digit factorial.
    sumOfAllDigitFactorial += factorial(lastDigit)
    newNumber = Math.floor(newNumber / 10)
  }
  // if the sumOftheFactorial is equal to the given number it means the number is a Krishnamurthy number.
  return sumOfAllDigitFactorial === number
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 参考

  • Check if a number is a Krishnamurthy Number or not - GeeksforGeeks (opens new window)
编辑 (opens new window)
上次更新: 2022/10/19, 17:35:33
BinaryConvert [二进制转换]
Coordinate

← BinaryConvert [二进制转换] Coordinate→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式