kotlin 打印堆栈_Kotlin程序打印给定数字的所有素数

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 打印堆栈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值