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-05-01
目录

ArmstrongNumber [阿姆斯特朗数]

# 介绍

阿姆斯特朗数,又称自恋者数(narcissist number)。在数字理论中,阿姆斯特朗数的定义是在任何给定的数基中,当它的每个数字被提升到该数字的位数的幂时,形成同一个数字的总数。

例如,使用一个简单的数字 153 和十进制系统,我们看到其中有 3 个数字。如果我们做一个简单的数学运算,把它的每个数字提高到 3 的幂,然后把得到的总和加起来,我们得到 153。也就是 1 到 3 的幂,5 到 3 的幂,3 到 3 的幂。这也可以表示为 1^3+5^3+3^3=153。数字 153 是一个阿姆斯特朗数字的例子,它有一个独特的属性,可以使用任何数字系统。

因此,如果在任何给定的数字系统中,将每个数字提高到该数字位数的幂数,然后相加得到一个数字,得到的总数等于原来的数字,这样的数字就被称为阿姆斯特朗数。

那么,有没有利用阿姆斯特朗数的实际应用呢?实际上,没有,这些数字除了作为验证程序的例子或学习工具,更好地学习概念和探索新编程语言的规则外,没有任何实际用途。

# 实现

# JavaScript

/**
 * An Armstrong number is equal to the sum of its own digits each raised to the power of the number of digits.
 * For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
 * An Armstrong number is often called Narcissistic number.
 */
const armstrongNumber = (num) => {
  if (num < 0 || typeof num !== 'number') return false

  let newSum = 0

  const numArr = num.toString().split('')
  numArr.forEach((num) => {
    newSum += parseInt(num) ** numArr.length
  })

  return newSum === num
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

写一个程序来寻找 0 和 999 范围内的所有阿姆斯特朗数字。

! ---------------------------------------------------------------
! This program computes all Armstrong numbers in the range of
! 0 and 999.  An Armstrong number is a number such that the sum
! of its digits raised to the third power is equal to the number
! itself.  For example, 371 is an Armstrong number, since
! 3**3 + 7**3 + 1**3 = 371.
! ---------------------------------------------------------------

PROGRAM  ArmstrongNumber
   IMPLICIT  NONE

   INTEGER :: a, b, c                   ! the three digits
   INTEGER :: abc, a3b3c3               ! the number and its cubic sum
   INTEGER :: Count                     ! a counter

   Count = 0
   DO a = 0, 9                          ! for the left most digit
      DO b = 0, 9                       !   for the middle digit
         DO c = 0, 9                    !     for the right most digit
            abc    = a*100 + b*10 + c   !        the number
            a3b3c3 = a**3 + b**3 + c**3 !        the sum of cubes
            IF (abc == a3b3c3) THEN     !        if they are equal
               Count = Count + 1        !           count and display it
               WRITE(*,*)  'Armstrong number ', Count, ': ', abc
            END IF
         END DO
      END DO
   END DO

// Armstrong number 1: 0
// Armstrong number 2: 1
// Armstrong number 3: 153
// Armstrong number 4: 370
// Armstrong number 5: 371
// Armstrong number 6: 407
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

# 参考

  • What is Armstrong Number? An Interesting Overview (2022) (opens new window)
编辑 (opens new window)
上次更新: 2022/10/18, 22:58:49
ArithmeticGeometricMean [算术-几何平均数]
AverageMean [平均数]

← ArithmeticGeometricMean [算术-几何平均数] AverageMean [平均数]→

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