📜  暴民数字

📅  最后修改于: 2021-04-29 15:43:29             🧑  作者: Mango

给定数字“ n”,任务是生成第一个“ n” Stormer数字。

斯托默数是一个正整数’i’,因此该项的最大素数i*i + 1大于或等于2*i
例如,5是一个斯托默数,因为最大质数26(即5 * 5 +1)是13,大于或等于10(即2 * 5)

    方法:
  1. 对于数字“ i”,首先找到数字i * i + 1的最大素数。
  2. 接下来,测试该素数因子是否大于或等于2 * i。
  3. 如果它更大,则“ i”是斯托默数。

下面是上述方法的实现:

C/C++
// C++ program to print 
// Stormer numbers 
// Function to find 
// largest prime factor
  
#include 
using namespace std;
  
 int maxPrimeFactors(int n) 
{ 
    // Initialize the maximum 
    // prime factor variable 
    // with the lowest one 
    int maxPrime = -1; 
  
    // Print the number of 
    // 2's that divide n 
    while(n % 2 == 0) 
    { 
        maxPrime = 2; 
        n /= 2; 
    } 
  
    // n must be odd at this 
    // point, thus skip the 
    // even numbers and iterate 
    // only for odd integers 
    for(int i = 3; i < (int)(n * 1 / 
                2 + 1); i += 2) 
        while(n % i == 0) 
        { 
            maxPrime = i; 
            n /= i; 
        } 
  
    // This condition is to handle 
    // the case when n is a prime 
    // number greater than 2 
    if (n > 2) 
        maxPrime = n; 
  
    return (int)(maxPrime); 
} 
  
// Function to generate 
// Stormer Numbers 
 int stormer(int n) 
{ 
    int i = 1; 
      
    // Stores the number of 
    // Stormer numbers found 
    int count = 0; 
    while(count < n) 
    { 
        int t = i * i + 1; 
        if (maxPrimeFactors(t) >= 2 * i) 
        { 
            cout << i ;
            cout <<" "; 
            count += 1; 
        } 
        i += 1; 
    } 
    return i; 
} 
  
    // Driver Code 
int main() {
  
    int n = 10; 
    stormer(n); 
  
    }


Java
// Java program to print 
// Stormer numbers 
  
// Function to find 
// largest prime factor 
  
import java.io.*;
  
class GFG {
static int maxPrimeFactors(int n) 
{ 
    // Initialize the maximum 
    // prime factor variable 
    // with the lowest one 
    int maxPrime = -1; 
  
    // Print the number of 
    // 2's that divide n 
    while(n % 2 == 0) 
    { 
        maxPrime = 2; 
        n /= 2; 
    } 
  
    // n must be odd at this 
    // point, thus skip the 
    // even numbers and iterate 
    // only for odd integers 
    for(int i = 3; i < (int)(n * 1 / 
                2 + 1); i += 2) 
        while(n % i == 0) 
        { 
            maxPrime = i; 
            n /= i; 
        } 
  
    // This condition is to handle 
    // the case when n is a prime 
    // number greater than 2 
    if (n > 2) 
        maxPrime = n; 
  
    return (int)(maxPrime); 
} 
  
// Function to generate 
// Stormer Numbers 
static int stormer(int n) 
{ 
    int i = 1; 
      
    // Stores the number of 
    // Stormer numbers found 
    int count = 0; 
    while(count < n) 
    { 
        int t = i * i + 1; 
        if (maxPrimeFactors(t) >= 2 * i) 
        { 
            System.out.print (i +" "); 
            count += 1; 
        } 
        i += 1; 
    } 
    return i;
}
  
    // Driver Code 
    public static void main (String[] args) {
      
    int n = 10; 
    stormer(n); 
  
    }
}
//This code is contributed akt_mit


Python3
# Python program to print Stormer numbers
  
from __future__ import print_function
  
# Function to find largest prime factor
  
def maxPrimeFactors(n):
    # Initialize the maximum prime factor
    # variable with the lowest one
    maxPrime = -1
  
    # Print the number of 2's that divide n
    while n % 2 == 0:
        maxPrime = 2
        n /= 2
  
    # n must be odd at this point, thus skip
    # the even numbers and iterate only for
    # odd integers
    for i in range(3, int(n**0.5)+1, 2):
        while n % i == 0:
            maxPrime = i
            n /= i
  
    # This condition is to handle the case when
    # n is a prime number greater than 2
    if n > 2:
        maxPrime = n
  
    return int(maxPrime)
  
# Function to generate Stormer Numbers
  
def stormer(n):
    i = 1
    # Stores the number of Stormer numbers found
    count = 0
    while(count < n):
        t = i * i + 1
        if maxPrimeFactors(t) >= 2 * i:
            print(i, end =' ')
            count += 1
        i += 1
  
# Driver Method
  
if __name__=='__main__':
    n = 10
    stormer(n)


C#
// C#  program to print 
// Stormer numbers 
using System;
  
// Function to find 
// largest prime factor 
public class GFG{
      
    static int maxPrimeFactors(int n) 
{ 
    // Initialize the maximum 
    // prime factor variable 
    // with the lowest one 
    int maxPrime = -1; 
  
    // Print the number of 
    // 2's that divide n 
    while(n % 2 == 0) 
    { 
        maxPrime = 2; 
        n /= 2; 
    } 
  
    // n must be odd at this 
    // point, thus skip the 
    // even numbers and iterate 
    // only for odd integers 
    for(int i = 3; i < (int)(n * 1 / 
                2 + 1); i += 2) 
        while(n % i == 0) 
        { 
            maxPrime = i; 
            n /= i; 
        } 
  
    // This condition is to handle 
    // the case when n is a prime 
    // number greater than 2 
    if (n > 2) 
        maxPrime = n; 
  
    return (int)(maxPrime); 
} 
  
// Function to generate 
// Stormer Numbers 
static int stormer(int n) 
{ 
    int i = 1; 
      
    // Stores the number of 
    // Stormer numbers found 
    int count = 0; 
    while(count < n) 
    { 
        int t = i * i + 1; 
        if (maxPrimeFactors(t) >= 2 * i) 
        { 
            Console.Write(i +" "); 
            count += 1; 
        } 
        i += 1; 
    } 
    return i; 
} 
  
    // Driver Code 
    static public void Main (){
            int n = 10; 
            stormer(n); 
  
    } 
} 
//This code is contributed akt_mit


PHP
 2)
        $maxPrime = $n;
  
    return (int)($maxPrime);
}
  
// Function to generate
// Stormer Numbers
function stormer($n)
{
    $i = 1;
      
    // Stores the number of 
    // Stormer numbers found
    $count = 0;
    while($count < $n)
    {
        $t = $i * $i + 1;
        if (maxPrimeFactors($t) >= 2 * $i)
        {
            echo $i." ";
            $count += 1;
        }
        $i += 1;
    }
}
  
// Driver Code
$n = 10;
stormer($n);
  
// This code is contributed
// by mits
?>


输出:
1 2 4 5 6 9 10 11 12 14