搜尋
首頁後端開發C++暴風雨數字

暴風雨數字

Aug 26, 2023 am 09:41 AM
編程 (programming)暴風雨 (storm)數字 (number)

暴風雨數字

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 print if it is greater than or equal to 2*N and keeping 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 −

  • 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 <iostream>
#include <bits/stdc++.h>

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<n;i++){  // for iterating the all number 
      int check=(i*i)+1;  // for storing the (i*i)+1 value in check
      if(prime_factor(check)>=(2*i)){ //for checking the number if maximum prime is greater or equal to 2*i
         cout<<i<<" ";
         temp++;
      }
   }
}
int main(){
   int n=9;
   stormer_number(n);
   
   return 0;
}

Output

#
1 2 4 5 6 9 10 11 12

結論

在本文中,我們嘗試解決列印前 N 個斯托默數的問題。

我們也學會了計算一個數的質因數。我希望這篇文章能幫助解決您對這個問題的所有疑惑。

以上是暴風雨數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
C#與C:歷史,進化和未來前景C#與C:歷史,進化和未來前景Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C#vs. C:學習曲線和開發人員的經驗C#vs. C:學習曲線和開發人員的經驗Apr 18, 2025 am 12:13 AM

C#和C 的学习曲线和开发者体验有显著差异。1)C#的学习曲线较平缓,适合快速开发和企业级应用。2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#vs. C:面向對象的編程和功能C#vs. C:面向對象的編程和功能Apr 17, 2025 am 12:02 AM

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显著差异。1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

從XML到C:數據轉換和操縱從XML到C:數據轉換和操縱Apr 16, 2025 am 12:08 AM

從XML轉換到C 並進行數據操作可以通過以下步驟實現:1)使用tinyxml2庫解析XML文件,2)將數據映射到C 的數據結構中,3)使用C 標準庫如std::vector進行數據操作。通過這些步驟,可以高效地處理和操作從XML轉換過來的數據。

C#vs. C:內存管理和垃圾收集C#vs. C:內存管理和垃圾收集Apr 15, 2025 am 12:16 AM

C#使用自動垃圾回收機制,而C 採用手動內存管理。 1.C#的垃圾回收器自動管理內存,減少內存洩漏風險,但可能導致性能下降。 2.C 提供靈活的內存控制,適合需要精細管理的應用,但需謹慎處理以避免內存洩漏。

超越炒作:評估當今C的相關性超越炒作:評估當今C的相關性Apr 14, 2025 am 12:01 AM

C 在現代編程中仍然具有重要相關性。 1)高性能和硬件直接操作能力使其在遊戲開發、嵌入式系統和高性能計算等領域佔據首選地位。 2)豐富的編程範式和現代特性如智能指針和模板編程增強了其靈活性和效率,儘管學習曲線陡峭,但其強大功能使其在今天的編程生態中依然重要。

C社區:資源,支持和發展C社區:資源,支持和發展Apr 13, 2025 am 12:01 AM

C 學習者和開發者可以從StackOverflow、Reddit的r/cpp社區、Coursera和edX的課程、GitHub上的開源項目、專業諮詢服務以及CppCon等會議中獲得資源和支持。 1.StackOverflow提供技術問題的解答;2.Reddit的r/cpp社區分享最新資訊;3.Coursera和edX提供正式的C 課程;4.GitHub上的開源項目如LLVM和Boost提陞技能;5.專業諮詢服務如JetBrains和Perforce提供技術支持;6.CppCon等會議有助於職業

c#vs. c:每種語言都擅長c#vs. c:每種語言都擅長Apr 12, 2025 am 12:08 AM

C#適合需要高開發效率和跨平台支持的項目,而C 適用於需要高性能和底層控制的應用。 1)C#簡化開發,提供垃圾回收和豐富類庫,適合企業級應用。 2)C 允許直接內存操作,適用於遊戲開發和高性能計算。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具