kotlin 查找id

A prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.

质数是大于1的自然数,不能通过将两个较小的自然数相乘而形成。

Given a range start and end, we have to print all prime numbers between start and end (including start and end).

给定范围startend ,我们必须打印startend之间的所有素数(包括startend )。

Example:

例:

    Input:
start = 1
end = 100
Output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

程序在Kotlin中查找给定范围内的质数 (Program to find prime numbers in a given range in Kotlin)

/**
* Kotlin Program to find out Prime Numbers between
* given Range(include START and END)
* A prime number is a whole number greater than 1
* whose only factors are 1 and itself.
* e.g 7, 11, 13, 17
*/
package com.includehelp.basic
import java.util.*
//Function to check Prime Number
fun findPrimeNo(number: Long): Boolean {
if(number<2) return false
for (i in 2.toLong()..number/2) {
if (number % i == 0.toLong()) {
return false
}
}
return true
}
//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 Prime  number in Range
for (i in start..end) {
if (findPrimeNo(i)) {
list.add(i)
}
}
println("Prime Numbers from $start to $end  : $list")
}

Output

输出量

Run 1:
Enter Start of Range  : 1
Enter End of Range  : 100
Prime Numbers from 1 to 100  : [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
---
Run 2:
Enter Start of Range  : 333
Enter End of Range  : 999
Prime Numbers from 333 to 999  : [337, 347, 349, 353, 359, 367, 373,
379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,
461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557,
563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929,
937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

翻译自: https://www.includehelp.com/kotlin/find-prime-numbers-between-given-range.aspx

kotlin 查找id

kotlin 查找id_Kotlin程序查找给定范围内的素数相关推荐

  1. kotlin 查找id_Kotlin程序查找矩阵的转置

    kotlin 查找id A transpose of a matrix is simply a flipped version of the original matrix. We can trans ...

  2. kotlin 查找id_Kotlin程序查找立方体区域

    kotlin 查找id A cube has 6 square faces, if edges length is side. Then the area of each square is side ...

  3. kotlin 查找id_Kotlin程序查找等边三角形的区域

    kotlin 查找id Formula to find area of Equilateral Triangle: area = ( 1.73 × side × side)/4 查找等边三角形面积的公 ...

  4. kotlin 查找id_Kotlin程序查找平行四边形的区域

    kotlin 查找id Formula to find area of Parallelogram: area = base*height 查找平行四边形面积的公式: area = base * he ...

  5. kotlin 查找id_Kotlin程序查找Sphere的体积

    kotlin 查找id Formula to find volume of Sphere: volume =(4/3)*PI*r^3 查找球体体积的公式: volume =(4/3)* PI * r ...

  6. kotlin 查找id_Kotlin程序查找圆柱体区域

    kotlin 查找id A cylinder is a three-dimensional structure which has circular bases parallel to each ot ...

  7. kotlin 查找id_Kotlin程序查找Square区域

    kotlin 查找id Formula to find area of Square: area = side*side 查找Square面积的公式: area = side * side Given ...

  8. kotlin 查找id_Kotlin程序在矩阵中查找偶数和奇数的频率

    kotlin 查找id Given a matrix, we have to find frequencies of even and odd numbers. 给定一个矩阵,我们必须找到偶数和奇数的 ...

  9. 7-1 查找书籍 (20 分)给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。输入格式:输入第一行给出正整数n(<10),随后给出n本书的信息

    7-1 查找书籍 (20 分) 给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价. 输入格式: 输入第一行给出正整数n(<10),随后给出n本书的信息.每本 ...

最新文章

  1. opencv-python 使用掩模抠图
  2. 关于Python的人工智能这么火,一大波交叉学科和技术应用正在靠近
  3. keepoutlayer设定_AD的keepout层是做什么用的,如何使用
  4. Shell字符串比较(等于、不等于、大于、小于、起始字符、结尾字符)
  5. 突发!5G 标准推迟三个月
  6. DBUtils注意问题
  7. java迭代器怎么用_Java中迭代器的使用
  8. mongodb默认的用户名密码_Linux如何安装、运行MongoDB教程
  9. Unity3D脚本语言的类型系统
  10. 射频识别技术漫谈(17)——射频卡中数据的存储形式
  11. C/C++回溯经典练习:马的走法
  12. chrome浏览器怎么把整个网页截图保存
  13. Elasticsearch:深入理解 Dissect ingest processor
  14. 使用Proxmox 和 Deskpool 搭建桌面云系统
  15. Unity学习4:如何实现2D图像跟踪(涂色类AR项目实践1)
  16. LabVIEW入门第六天(布尔控件及布尔量)
  17. 轻量级网络模型MobileNet发展脉络(V1-V2-V3)
  18. 9.二重循环:什么是二重循环???
  19. EasyUI的插入一行到某行和添加一行
  20. Python黑技术:远程控制男朋友电脑

热门文章

  1. Veil生成免杀payload 渗透win10 获取靶机shell
  2. 云计算系统是大规模计算机系统吗,云计算的系统架构及技术探析
  3. java httpserver 多个接口_多个Servlet之间数据共享实现方案
  4. VUE v-bind绑定class和style
  5. js实现QQ、微信、新浪微博分享功能
  6. 异步导致UI句柄增加的解决办法
  7. 微信群运营之设计运营思路
  8. TMS320F28335——IO控制/定时计操作
  9. [TypeScript] Export public types from your library
  10. 分布式Session框架