# 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 is greater than or equal to .
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++

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

Output:

```1 2 4 5 6 9 10 11 12 14
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6740145,"math_prob":0.9869343,"size":8042,"snap":"2020-34-2020-40","text_gpt3_token_len":2542,"char_repetition_ratio":0.1688231,"word_repetition_ratio":0.40080506,"special_character_ratio":0.34705296,"punctuation_ratio":0.09108911,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99969816,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,3,null,3,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-21T16:13:33Z\",\"WARC-Record-ID\":\"<urn:uuid:be66daa1-58a8-474d-97e5-8f9dd55862c6>\",\"Content-Length\":\"163215\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3350c558-9a73-443b-963a-dd509b6cd3d8>\",\"WARC-Concurrent-To\":\"<urn:uuid:a26ad03f-2bd5-4f99-b472-b41e3a3655ad>\",\"WARC-IP-Address\":\"104.96.221.209\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/stormer-numbers/\",\"WARC-Payload-Digest\":\"sha1:R5RO7LZIYXEPBLXKK2YTTT7DEZWHKISJ\",\"WARC-Block-Digest\":\"sha1:ZUF5XXLGSK3O225WEL2H3ZF2WKCJJOTX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400201826.20_warc_CC-MAIN-20200921143722-20200921173722-00263.warc.gz\"}"}