kotlin 打印堆栈
Prime factors are factors of a number which are prime numbers.
素因子被它是素数的一些因素。
Given an integer number, we have to print it's all prime factors.
给定一个整数,我们必须打印所有素数。
Example:
例:
Input:
50
Output:
2, 5
In the below program, we are creating a Kotlin program to print all prime factors of the given number.
在下面的程序中,我们将创建一个Kotlin程序来打印给定数字的所有素数 。
程序在Kotlin中打印给定数字的所有素数 (Program to print all prime factors of given number in Kotlin)
package com.includehelp.basic
import java.util.*
/**
* Function to find prime factor for supplied number
* @param number
* @return
*/
fun getPrimeFactors(number: Long): String {
var number = number
//set not Allowed Duplicate element
val setPrimeFactors: MutableSet<Int> = HashSet()
var i = 2
while (i <= number) {
if (number % i == 0L) {
// Add prime factor in Hash Set
setPrimeFactors.add(i)
number /= i
i--
}
i++
}
return setPrimeFactors.toString()
}
//Main Function entry Point of Program
fun main(args: Array<String>) {
val sc = Scanner(System.`in`)
println("Enter Number : ")
val number: Int = sc.nextInt() // Input Number
//Print Primary Factor
println("Prime Factors of $number is : ${getPrimeFactors(number.toLong())} ")
}
Output
输出量
Run 1:
Enter Number :
50
Prime Factors of 50 is : [2, 5]
-------
Run 2:
Enter Number :
345345
Prime Factors of 345345 is : [3, 5, 7, 23, 11, 13]
翻译自: https://www.includehelp.com/kotlin/print-all-prime-factors-of-given-number.aspx
kotlin 打印堆栈