Bei einer gegebenen Zahl „n“ besteht die Aufgabe darin, die ersten „n“ Stormer-Zahlen zu generieren.
Eine Stormer-Zahl ist eine positive ganze Zahl „i“, bei der der größte Primfaktor des Terms  i*i + 1  größer oder gleich ist  2 * ich
Zum Beispiel ist 5 eine Stormer-Zahl, weil der größte Primfaktor von 26 (dh 5*5 + 1) 13 ist, was größer oder gleich 10 ist (dh 2*5). 
 

Eingabe:
Ausgabe: 1 2 4 5 6 
Hier ist 3 keine Stormer-Zahl, da der größte Primfaktor 
von 10 (dh 3*3 + 1) 5 ist, was nicht größer 
oder gleich 6 ist (dh 2*3)
Eingang: 10 
Ausgang: 1 2 4 5 6 9 10 11 12 14 
 

  1. Finde für eine Zahl „i“ zuerst den größten Primfaktor der Zahl i*i + 1.
  2. Testen Sie als Nächstes, ob dieser Primfaktor größer oder gleich 2*i ist.
  3. Wenn es größer ist, dann ist 'i' eine Stormer-Zahl.

Unten ist die Implementierung des obigen Ansatzes:
 

C++

// C++ program to print
// Stormer numbers
// Function to find
// largest prime factor
 
#include <iostream>
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

<?php
// PHP program to print
// Stormer numbers
 
// Function to find
// largest prime factor
function 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 = 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
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
?>

Javascript

<script>
    // Javascript program to print Stormer numbers
     
    // Function to find largest prime factor
    function maxPrimeFactors(n)
    {
     
        // Initialize the maximum
        // prime factor variable
        // with the lowest one
        let maxPrime = -1;
 
        // Print the number of
        // 2's that divide n
        while(n % 2 == 0)
        {
            maxPrime = 2;
            n = parseInt(n / 2, 10);
        }
 
        // n must be odd at this
        // point, thus skip the
        // even numbers and iterate
        // only for odd integers
        for(let i = 3; i < (n * 1 / 2 + 1); i += 2)
            while(n % i == 0)
            {
                maxPrime = i;
                n = parseInt(n / i, 10);
            }
 
        // This condition is to handle
        // the case when n is a prime
        // number greater than 2
        if (n > 2)
            maxPrime = n;
 
        return (maxPrime);
    }
 
    // Function to generate
    // Stormer Numbers
    function stormer(n)
    {
        let i = 1;
 
        // Stores the number of
        // Stormer numbers found
        let count = 0;
        while(count < n)
        {
            let t = i * i + 1;
            if (maxPrimeFactors(t) >= 2 * i)
            {
                document.write(i +" ");
                count += 1;
            }
            i += 1;
        }
        return i;
    }
     
    let n = 10;
    stormer(n);
 
// This code is contributed by rameshtravel07.
</script>
Ausgabe: 
1 2 4 5 6 9 10 11 12 14

 

Falls Sie an Live-Kursen mit Experten teilnehmen möchten , beziehen Sie sich bitte auf DSA Live-Kurse für Berufstätige und Competitive Programming Live for Students .