0

0

暴风雨数字

PHPz

PHPz

发布时间:2023-08-26 09:41:17

|

1430人浏览过

|

来源于tutorialspoint

转载

暴风雨数字

For N to be a stormer number, the highest prime factor of the expression N^2+1 must be greater than or equal to 2*N and it should be a positive integer.

For example, 4 is a stormer number. Since 4*4+1=17 has the greatest prime factor 17 itself which is greater than 8 i.e. 2*4.

但是3不是一个强力数字,因为3*3+1=10。10的最大质因数是5,小于6即2*3。

在这个问题中,我们给定一个正整数N,我们的目标是打印出前N个stormer。

输入: 4

OUTPUT: 1 2 4 5

这里是前4个斯托默数。3不是斯托默数,所以没有包括在内。

算法

  • 找到数字(N^2+1)的最大质因数,并将其存储在任意变量中。

  • Check if the prime factor is larger than or equal to 2*N.

  • If it satisfies the condition hence it is a stormer number.

  • Print all the stormer numbers until i is less than or equal to N.

方法

To implement the above algorithm in our code, we need to make two functions. First, to find out the highest prime factor for each case and second to check if it is greater than or equal to 2*N and keep printing the number if it is a stormer number simultaneously.

For finding out the highest prime factor of expression (n^2+1) for every number n −

笔灵降AI
笔灵降AI

论文降AI神器,适配知网及维普!一键降至安全线,100%保留原文格式;无口语化问题,文风更学术,降后字数控制最佳!

下载
  • We will divide the number by 2 until it gives remainder 0 and store 2 in primemax.

  • 现在,n在这一点上必须是奇数,所以我们将在一个for循环中迭代,只迭代从i=3到n的平方根的奇数。

  • Now store i in primemax and divide n by i while i divides n. When i fails to divide n then raise it by 2 and carry on.

  • 如果n是一个大于2的质数,那么在前两个步骤中n不会变成1,因此我们将n存储在primemax中并返回primemax。

The next function will be to check if the number is a stormer number or not. If it is, we will print it.

  • 我们将声明一个变量 temp 为 0,以计算前 N 个 stormer 数。

  • 从i=1开始,在temp小于N之前进行for循环迭代。

  • 检查 (i*i+1) 是否具有大于或等于 2*i 的最大质因数。如果上述条件为真,则打印 i 并将 temp 增加 1。

Example

的中文翻译为:

示例

Below is the implementation of above approach in C++ −

#include 
#include 

using namespace std;

int prime_factor(int n){ //for finding the maximum prime factor
   int primemax=0;
   while(n%2==0){ //if n is divided by 2 than we store 2 in primemax 
      primemax=2;
      n=n/2;
   }
   for(int i=3;i<=sqrt(n);i+=2){ // this is only for odd number 
      while(n%i==0){
         primemax=i;
         n=n/i;
      }
   }
   if(n>2){ // when n is prime number and greater than 2 
      primemax=n;
   }
   return primemax;
}
void stormer_number(int n){  //function to print first n stormer numbers
   int temp=0; // for counting the stormer number 
   for(int i=1;temp=(2*i)){ //for checking the number if maximum prime is greater or equal to 2*i
         cout<

Output

1 2 4 5 6 9 10 11 12

结论

在本文中,我们尝试解决打印前 N 个斯托默数的问题。

我们还学会了计算一个数的质因数。我希望这篇文章能帮助解决您对这个问题的所有疑惑。

相关专题

更多
Sass和less的区别
Sass和less的区别

Sass和less的区别有语法差异、变量和混合器的定义方式、导入方式、运算符的支持、扩展性等。本专题为大家提供Sass和less相关的文章、下载、课程内容,供大家免费下载体验。

197

2023.10.12

python中print函数的用法
python中print函数的用法

python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

181

2023.09.27

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

703

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

80

2023.09.25

function是什么
function是什么

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

468

2023.08.04

js函数function用法
js函数function用法

js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

158

2023.10.07

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

381

2023.08.14

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

115

2025.12.24

拼豆图纸在线生成器
拼豆图纸在线生成器

拼豆图纸生成器有PixelBeads在线版、BeadGen和“豆图快转”;推荐通过pixelbeads.online或搜索“beadgen free online”直达官网,避开需注册的诱导页面。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

84

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Rust 教程
Rust 教程

共28课时 | 3.8万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号