Stormer Numbers
Last Updated :
06 Mar, 2023
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
- For a number ‘i’, first find the largest prime factor of the number i*i + 1.
- Next, test whether that prime factor is greater than or equal to 2*i.
If it is greater then ‘i’ is a Stormer number.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPrimeFactors( int n)
{
int maxPrime = -1;
while (n % 2 == 0)
{
maxPrime = 2;
n /= 2;
}
for ( int i = 3; i < ( int )( sqrt (n)+ 1); i += 2)
while (n % i == 0)
{
maxPrime = i;
n /= i;
}
if (n > 2)
maxPrime = n;
return ( int )(maxPrime);
}
int stormer( int n)
{
int i = 1;
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;
}
int main() {
int n = 10;
stormer(n);
}
|
Java
import java.io.*;
class GFG {
static int maxPrimeFactors( int n)
{
int maxPrime = - 1 ;
while (n % 2 == 0 )
{
maxPrime = 2 ;
n /= 2 ;
}
for ( int i = 3 ; i < ( int )(Math.sqrt(n)+ 1 ); i += 2 )
while (n % i == 0 )
{
maxPrime = i;
n /= i;
}
if (n > 2 )
maxPrime = n;
return ( int )(maxPrime);
}
static int stormer( int n)
{
int i = 1 ;
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;
}
public static void main (String[] args) {
int n = 10 ;
stormer(n);
}
}
|
Python3
from __future__ import print_function
def maxPrimeFactors(n):
maxPrime = - 1
while n % 2 = = 0 :
maxPrime = 2
n / = 2
for i in range ( 3 , int (n * * 0.5 ) + 1 , 2 ):
while n % i = = 0 :
maxPrime = i
n / = i
if n > 2 :
maxPrime = n
return int (maxPrime)
def stormer(n):
i = 1
count = 0
while (count < n):
t = i * i + 1
if maxPrimeFactors(t) > = 2 * i:
print (i, end = ' ' )
count + = 1
i + = 1
if __name__ = = '__main__' :
n = 10
stormer(n)
|
C#
using System;
public class GFG{
static int maxPrimeFactors( int n)
{
int maxPrime = -1;
while (n % 2 == 0)
{
maxPrime = 2;
n /= 2;
}
for ( int i = 3; i < ( int )(Math.Sqrt(n)+ 1); i += 2)
while (n % i == 0)
{
maxPrime = i;
n /= i;
}
if (n > 2)
maxPrime = n;
return ( int )(maxPrime);
}
static int stormer( int n)
{
int i = 1;
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;
}
static public void Main (){
int n = 10;
stormer(n);
}
}
|
PHP
<?php
function maxPrimeFactors( $n )
{
$maxPrime = -1;
while ( $n % 2 == 0)
{
$maxPrime = 2;
$n /= 2;
}
for ( $i = 3; $i < (int)(sqrt( $n )+ 1); $i += 2)
while ( $n % $i == 0)
{
$maxPrime = $i ;
$n /= $i ;
}
if ( $n > 2)
$maxPrime = $n ;
return (int)( $maxPrime );
}
function stormer( $n )
{
$i = 1;
$count = 0;
while ( $count < $n )
{
$t = $i * $i + 1;
if (maxPrimeFactors( $t ) >= 2 * $i )
{
echo $i . " " ;
$count += 1;
}
$i += 1;
}
}
$n = 10;
stormer( $n );
?>
|
Javascript
<script>
function maxPrimeFactors(n)
{
let maxPrime = -1;
while (n % 2 == 0)
{
maxPrime = 2;
n = parseInt(n / 2, 10);
}
for (let i = 3; i < parseInt(Math.sqrt(n)+ 1); i += 2)
while (n % i == 0)
{
maxPrime = i;
n = parseInt(n / i, 10);
}
if (n > 2)
maxPrime = n;
return (maxPrime);
}
function stormer(n)
{
let i = 1;
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);
</script>
|
Output: 1 2 4 5 6 9 10 11 12 14
Time Complexity: O(k * sqrt(k)) where k is the nth Stormer number.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...