风暴号

给定一个数字“n”,任务是生成第一个“n”风暴号。 A. 风暴号 是一个正整数’i’,使得该项的最大素因子 i*i + 1  大于或等于 2*i  . 例如,5是风暴数,因为26(即5*5+1)的最大素因子是13,大于或等于10(即2*5)

null

输入: 5. 输出: 1 2 4 5 6 这里3不是风暴数,因为最大素数 10的系数(即3*3+1)为5,不大于 或等于6(即2*3) 输入: 10 输出: 1 2 4 5 6 9 10 11 12 14

  1. 对于一个数“i”,首先求出i*i+1的最大素因子。
  2. 接下来,测试该素因子是否大于或等于2*i。
  3. 如果大于,则“i”是风暴号。

以下是上述方法的实施情况:

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>


输出:

1 2 4 5 6 9 10 11 12 14

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享