Stormer Numbers

Given a number ‘n’, the task is to generate the first ‘n’ Stormer numbers.

A Stormer Number is a positive integer ‘i’ such that the greatest prime factor of the term i*i + 1 is greater than or equal to 2*i.
For example, 5 is a Stormer number because the greatest prime factor of 26(i.e 5*5 + 1) is 13 which is greater than or equal to 10(i.e 2*5)

Input : 5
Output : 1 2 4 5 6
Here 3 is not a Stormer number because the greatest prime
factor of 10(i.e 3*3 + 1) is 5, which is not greater than
or equal to 6(i.e 2*3)



Input : 10
Output : 1 2 4 5 6 9 10 11 12 14

    Approach:

  1. For a number ‘i’, first find the largest prime factor of the number i*i + 1.
  2. Next, test whether that prime factor is greater than or equal to 2*i.
  3. If it is greater then ‘i’ is a Stormer number.

Below is the implementation of above approach:

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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); 
  
    

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

1 2 4 5 6 9 10 11 12 14


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, jit_t