埃拉托色尼筛法(Sieve of Eratosthenes)算法的Java实现

Java 专栏收录该内容
¥59.90 ¥99.00
234篇文章

埃拉托色尼筛法(Sieve of Eratosthenes)算法的Java实现

埃拉托色尼筛法(Sieve of Eratosthenes)是一种用于求解素数的古老而有效的算法。它的基本思想是通过逐步筛选掉非素数的方式来找到所有的素数。本文将详细介绍如何使用Java语言实现埃拉托色尼筛法算法,并提供相应的源代码。

算法原理:

  1. 从2开始,将所有的数标记为素数。
  2. 从2开始,将所有的倍数标记为非素数。
  3. 找到下一个未被标记为非素数的数,将其作为新的素数。
  4. 重复步骤2和3,直到达到预定的上限。

以下是用Java实现埃拉托色尼筛法算法的源代码:

import java.util.*;

public class SieveOfEratos
找质数算法之埃拉托色尼筛选法(Sieve of Eratosthenes算法) 一、算法原理 一个合数总是可以分解成若干个质数的乘积,那么如果把质数(最初只知道2是质数)的倍数都去掉,那么剩下的就是质数了。 二、步骤 (1)先把1删除(1既不是质数也不是合数) (2)读取队列中当前最小的数2,然后把2的倍数删去 (3)读取队列中当前最小的数3,然后把3的倍数删去 (4)读取队列中当前最小的数5,然后把5的倍数删去 ....... (n) 阅读详情

相关推荐

Sieve Of Eratosthenes 算法:高效打印素数的实现(JavaScript)

埃拉托色尼筛法是一种高效的素数查找算法,可以快速找出指定范围内的所有素数。这个算法在编程和数学领域都有广泛的应用,对于解决与素数相关的问题非常有帮助。本文将介绍一种高效的素数查找算法——埃拉托色尼筛法(Sieve Of Eratosthenes),并使用 JavaScript 来实现该算法。埃拉托色尼筛法是一种高效的素数查找方法,时间复杂度为 O(n log log n)。将数组的第一个和第二个元素(代表数字 0 和 1)标记为 false,因为它们不是素数。,打印值为 true 的索引,即为素数。

TechNovaX的博客 153

JAVA:实现Sieve Of Eratosthenes埃拉托色尼筛法算法(附完整源码)

JAVA:实现Sieve Of Eratosthenes埃拉托色尼筛法算法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 520

java实现埃拉托色尼筛法(附完整源码)

java实现埃拉托色尼筛法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 271

Java:实现用埃拉托色尼的筛子找出一定限度内的所有质数算法(附完整源码)

Java:实现用埃拉托色尼的筛子找出一定限度内的所有质数算法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 520

Python sieve of Eratosthenes埃拉托色尼筛法算法详解及源码

Sieve of Eratosthenes(埃拉托色尼筛法)是一种用于找出一定范围内所有素数的有效算法。它的原理很简单:首先,将从2开始的所有数标记为素数,然后从2开始,将其所有倍数标记为非素数。接下来,找到下一个未标记为非素数的数并重复这个过程,直到完成。

希望我的博客,能帮上你解决学习中工作中所遇到的问题 404

机械拆装-基于Unity-总体设计

建立一个机械装配的场景

唐小墨同学的博客 2776

【AI概念】迁移学习 vs. 域自适应 vs. 域泛化|定义、核心原理、数学表达、易混淆点与工程选择建议|实际案例、工程实现细节、未来研究趋势、实际应用场景

大家好,我是爱酱。本篇将会详细梳理迁移学习(Transfer Learning)、域自适应(Domain Adaptation)、域泛化(Domain Generalization)三大常被混淆的核心概念,结合数学公式、典型算法与实际应用场景,帮助你厘清它们的本质区别、联系与工程意义。 注:本文章含大量数学算式、详细例子说明及大量代码演示,大量干货,建议先收藏再慢慢观看理解。新频道发展不易,你们的每个赞、收藏跟转发都是我继续分享的动力!

AI人工智能爱酱~你的AI学习好帮手~ 2470

Palantir的本体论建模跟数仓建模的异同

Palantir的Foundry Ontology通过构建组织的数字孪生体,将数据集和模型映射为对象类型、属性、链接和操作类型,形成完整的语义知识网络。与传统数仓建模相比,本体建模以业务问题为驱动,强调细粒度语义和实时决策能力,采用图语义技术实现灵活的映射关系。

轩哥的个人研究和成长记录 1596

基于Simulink开发一个完整的直驱式永磁同步发电机DDPMSG仿真模型

需求分析直驱式永磁同步发电机(Direct-Drive Permanent Magnet Synchronous Generator, DDPMSG)广泛应用于风力发电、海洋能发电等可再生能源领域。与传统的齿轮箱驱动系统相比,DDPMSG具有结构简单、效率高、可靠性好等优点,能够直接将机械能转换为电能,减少了中间传动环节,降低了维护成本。本项目将基于Simulink平台,开发一个完整的DDPMSG仿真模型,模拟其在风力发电场景中的应用。我们将详细讲解如何设计和实现该系统,并结合实际应用案例进行说明。

MHD0815的博客 465

RT-Thread必考面试题及参考答案

RT-Thread的设备驱动模型遵循层次化的结构,设计简洁且易于扩展。它主要由设备注册、设备操作和设备控制三部分组成。设备驱动模型允许开发者为不同的硬件设备编写驱动程序,这些驱动程序可以被RT-Thread内核统一管理和调度。在RT-Thread中,设备被抽象成一系列标准的接口,包括打开、关闭、读取、写入、控制等操作,这些操作封装在结构体中。设备驱动程序需要实现这些接口,然后通过调用函数注册设备,使其对系统可见。这种模型不仅简化了驱动开发,还提高了代码的可移植性和可维护性。

大模型大数据攻城狮的专栏 1823

银河麒麟高级服务器V11操作系统安装部署zabbix监控平台的实操保姆级教程

本文详细介绍了在《银河麒麟高级服务器V11上安部署Zabbix监控系统的全过程》涵盖了zabbix监控系统涉及到的所有软件安装、参数优化、服务启动验证及防火墙配置等一系列实操。其中关于安装过程报错undefined symbol: usmAES256CiscoPrivProtocol;ares_reinit等依赖库问题进行了详细记录与分析解决;还提供了Web前端中文显示修复方案和Admin密码修改方法。实现监控平台的100%部署上线,实现对业务系统的实时监控,保证业务的稳定运行。

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 531

AEJoy —— 表达式之仅限在某一维摆动【JS】

效果图 wiggle() 的作用仅限于水平方向 想法 wiggle() 确实是一个非常方便的工具,但有时您希望对它有更多的控制。在本例中,我们希望仅将摆动限制在水平方向。 设计 要理解 wiggle() 最重要的一点是,它生成的结果与应用它的属性具有相同的维度。例如,如果你将 wiggle() 应用于 3D 层的 position 属性,结果将是一个由三个元素组成的数组(x、y 和 z 各一个)。我们需要做的就是将 wiggle() 的结果捕捉到一个变量(我们称之为 “w” ),然后从这个变量中提取我们想

ShaderJoy 的兴趣技术杂货铺 2094

openssl命令数据摘要详解(SM3、sha256、MD5、dss1、mdc2、ripemd160)

SM3是由中国密码学家设计的密码哈希函数,是中国国家密码管理局(National Cryptographic Authority,简称:CNCA)颁布的国家密码算法标准。SM3属于密码杂凑函数家族,它可以计算任意长度的消息并生成固定长度(256比特,即32字节)的哈希值。SM3生成的哈希值长度为256比特(32字节),不管输入消息的长度如何,输出始终是固定的。SM3将输入数据划分为512比特(64字节)的分组,并对每个分组进行处理。

N阶二进制的博客 4460

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

上一篇: Java实现:递归实现二叉搜索树算法
下一篇: 分布式缓存:基于Java的缓存淘汰算法实现
普通网友
博客等级 码龄3年 62粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值