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

2021-04-28 17:53:37 浏览数 (1)

参考链接: 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 判断数字

0 人点赞