Números de Stormer

Dado un número ‘n’, la tarea es generar los primeros números ‘n’ Stormer.
Un Número de Stormer es un entero positivo ‘i’ tal que el mayor factor primo del término  i*i + 1  es mayor o igual que  2*i
Por ejemplo, 5 es un número de Stormer porque el mayor factor primo de 26 (es decir, 5*5 + 1) es 13, que es mayor o igual que 10 (es decir, 2*5) 
 

Entrada:
Salida: 1 2 4 5 6 
Aquí 3 no es un número de Stormer porque el mayor 
factor primo de 10 (es decir, 3*3 + 1) es 5, que no es mayor 
ni igual a 6 (es decir, 2*3)
Entrada: 10 
Salida: 1 2 4 5 6 9 10 11 12 14 
 

  1. Para un número ‘i’, primero encuentre el factor primo más grande del número i*i + 1.
  2. Luego, prueba si ese factor primo es mayor o igual a 2*i.
  3. Si es mayor, ‘i’ es un número de Stormer.

A continuación se muestra la implementación del enfoque anterior:
 

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>
Producción: 

1 2 4 5 6 9 10 11 12 14

 

Publicación traducida automáticamente

Artículo escrito por SaagnikAdhikary y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *