Java例题_27 100以内的素数
- 1 /*27 【程序 27 求素数】
- 2 题目:求 100 之内的素数
- 3 */
- 4
- 5 /*分析
- 6 * 素数:是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
- 7 * 同第二题:
- 8 * 判断素数的方法:用这个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明此数不是素
- 9 数,反之是素数————————sqrt() double 要强制转换---经过试验,系统会自动转换
- 10 * 两层for循环,第一层遍历2~100,第二层判断是否为素数
- 11 * */
- 12
- 13
- 14 package homework;
- 15
- 16 public class _27 {
- 17
- 18 public static void main(String[] args) {
- 19 //声明一个标记,标注是否为素数
- 20 boolean isPrime=true;
- 21 System.out.print("100以内的素数有:2 ");
- 22 //遍历3~100
- 23 for (int i = 3; i <= 100; i++) { //2是素数,为了简便判断,直接从3开始
- 24 //判断是否为素数
- 25 for (int j = 2; j <=(Math.sqrt(i)); j++) {
- 26 if(i%j==0) {
- 27 isPrime=false;
- 28 break;
- 29 }
- 30 else {
- 31 isPrime=true;
- 32 }
- 33 }
- 34 if(isPrime==true) {
- 35 System.out.print(i+" ");
- 36 }
- 37 }
- 38
- 39 }
- 40
- 41 }
Java例题_27 100以内的素数的更多相关文章
- 求出100以内的素数(java实现)
j package test1; //2018/11/30 //求100以内的所有素数 public class Main10 { public static void main(String[] a ...
- 斐波那契数列(递归)&求100以内的素数
Java 5 添加了 java.util.Scanner 类,这是一个用于扫描输入文本的新的实用程序.它是以 前的 StringTokenizer 和 Matcher 类之间的某种结合.由于任何数据都 ...
- 1.2输出100以内的素数&输出前100个素数。
输出100以内的素数只是一个嵌套,在1.1的基础上添加一层循环,只需要注意从2开始,并且变量需要换一个. #include<stdio.h> int main() { ; ; i < ...
- Python练习题 026:求100以内的素数
[Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...
- 用python计算100以内的素数
用python计算100以内的素数 : break else: list.append(i)print(list)
- python、C++经典算法题:打印100以内的素数
题目 打印100以内的素数 思路1 素数的特点: 素数一定是奇数 一个数如果是合数,那么它一定能够被2到这个合数的开平方内的某个素数整除(这个特点是提升效率的关键) 一个数如果不能被从2到它自身开平方 ...
- 实现100以内的素数输出(Python与C++对比)
今天从链接http://www.2cto.com/kf/201302/187699.html中看到了Python实现100以内的素数输出的算法,颇受感触.尤其是被其中的Python的列表生成器的使用方 ...
- oracle中plsql练习-----在控制台输出1到100以内的素数。
一.思路:首先需要知道素数的概念即质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 中心思想是,外循环所有的自然数,内循环折半查询,增加代码的速度,注意:从1开始,需要大于1,但是pl ...
- js求100以内的素数
//打印2~100之间的数 ; i< ; i++){ var a = true; ; j < i; j++){ //判断i能否被j整除 ){ //能被整除则说明不是素数,修改布尔值为fal ...
随机推荐
- 免费在线 Linux Desktop 环境
免费在线 Linux Desktop 环境 Run Linux OS Distributions online https://www.onworks.net/os-distributions 免费测 ...
- Chart.js & CPU 性能监控
Chart.js 可视化动态 CPU 性能监控 https://github.com/gildata/RAIO/issues/337 https://github.com/chartjs/Chart. ...
- how to read the 10th line of a text using shell script
how to read the 10th line of a text using shell script shell script / bash script question https://l ...
- leetcode solution cracked tutorial
leetcode solution cracked tutorial problemset https://leetcode.com/problemset/all/ Top Interview Que ...
- js group objects in an array
js group objects in an array js group objects in an array var groupBy = function(xs, key) { return x ...
- css & circle & shapes
css & circle & shapes css-tricks circle https://css-tricks.com/the-shapes-of-css/ https://cs ...
- c++ 获取当前程序的主模块句柄
char text[2014]; GetModuleBaseNameA(GetCurrentProcess(), 0, text, 1024); HMODULE hModule = GetModule ...
- MongoDB的下载、安装与部署
1.什么是MongoDB? 它是介于关系型数据库和非关系型数据库之间的一种NoSQL数据库,用C++编写,是一款集敏捷性.可伸缩性.扩展性于一身的高性能的面向文档的通用数据库. 2.为什么要用Mong ...
- (转载)VoLTE简介
转载地址:http://www.360doc.cn/article/2909773_637471256.html,本文介绍了移动通信领域相关概念,如CS.PS.VoIP.VoLTE.IMS.CSFB. ...
- Kubernetes和docker----1.开始使用k8s和docker
开始使用Kubernetes和docker docker命令 运行一个容器 docker run busybox echo "Hello world" 构建容器镜像 docker ...