前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >kotlin 判断数字_Kotlin程序显示范围之间的阿姆斯壮数字

kotlin 判断数字_Kotlin程序显示范围之间的阿姆斯壮数字

作者头像
用户7886150
修改2021-04-28 17:53:37
修改2021-04-28 17:53:37
1.2K0
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Java程序在两个间隔之间显示Armstrong数

kotlin 判断数字

 An Armstrong number is a number such that the sum of the nth power of its digits is equal to the number itself, where n is the number of digits in the number (taken here to mean positive integer). 

  阿姆斯特朗数是一个数字,使得其数字的n 次幂之和等于数字本身,其中n是数字中的数字位数(此处表示正整数)。  

     abcd = a^n+b^n+c^n+d^n

 Given two numbers start and end, we have to display the list of Armstrong numbers between start and end. 

  给定两个数字开头和结尾 ,我们必须显示开始和结尾之间的Armstrong号码列表。  

 Example: 

  例:  

     Input:

    start = 15

    end = 700

    Output:

    [153, 370, 371, 407]

  程序以显示Kotlin范围内的阿姆斯壮数字 (Program to display Armstrong numbers between a range in Kotlin) 

 /**

    * Kotlin Program to check find out ArmStrong number 

    * between given Range(include START and END)

 */

package com.includehelp.basic

import java.util.*

// Function for check Armstrong number

fun findArmStrongNo(number: Long): Boolean {

    var isArmNumber = false

    var result : Long= 0

    var original = number

    //Count No Digits in numbers

    val digits = original.toString().length

    while (original > 0) {

        val r = original % 10

        result +=Math.pow(r.toDouble(), digits.toDouble()).toLong()

        original /= 10

    }

    if (result == number) {

        isArmNumber = true

    }

    return isArmNumber

}

//Main Function, Entry Point of Program

fun main(arg: Array<String>) {

    //Input Stream

    val sc = Scanner(System.`in`)

    //Input Start of Range

    print("Enter Start of Range  : ")

    val start: Long = sc.nextLong()

    //Input End of Range

    print("Enter End of Range  : ")

    val end: Long = sc.nextLong()

    //Declare Mutable List to hold factors

    val list: MutableList<Long> = ArrayList()

    //iterate through loop start to end to find ArmStrong in Range

    for (i in start..end) {

        if (findArmStrongNo(i)) {

            list.add(i)

        }

    }

    println("Armstrong Numbers from $start to $end  : $list")

}

 Output 

  输出量  

 RUN 1:

Enter Start of Range  : 15

Enter End of Range  : 700

Armstrong Numbers from 15 to 700  : [153, 370, 371, 407]

---

RUN 2:

Enter Start of Range  : 99

Enter End of Range  : 999999

Armstrong Numbers from 99 to 999999  : [153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834]

  翻译自: https://www.includehelp.com/kotlin/display-armstrong-numbers-between-a-range.aspx

 kotlin 判断数字

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档