En teoría de números, un número de Leyland es un número de la forma x y + y x , donde x e y son números enteros mayores que 1 y 1 <y <= x.
Dado un entero positivo N . La tarea es imprimir el primer número de N Leyland en orden ascendente. Los primeros números de Leyland son 8, 17, 32, 54, 57, 100, …
Ejemplos:
Input : N = 1 Output : 8 22 + 22 = 4 + 4 = 8. Input : N = 6 Output : 100
La idea de ejecutar dos bucles, uno para x y otro para y. El ciclo externo comienza con 2 an y para cada iteración del ciclo externo, ejecute el inicio del ciclo interno desde 2 t x. Y almacene x y + y x en una array. Después de calcular todo el valor, ordénelos e imprima los primeros n números.
A continuación se muestra la implementación de este enfoque:
C++
// CPP program to print first N Leyland Numbers. #include <bits/stdc++.h> #define MAX 100 using namespace std; // Print first n Leyland Number. void leyland(int n) { vector<int> ans; // Outer loop for x from 2 to n. for (int x = 2; x <= n; x++) { // Inner loop for y from 2 to x. for (int y = 2; y <= x; y++) { // Calculating x^y + y^x int temp = pow(x, y) + pow(y, x); ans.push_back(temp); } } // Sorting the all Leyland Number. sort(ans.begin(), ans.end()); // Printing first n Leyland number. for (int i = 0; i < n; i++) cout << ans[i] << " "; } // Driven Program int main() { int n = 6; leyland(n); return 0; }
Java
// Java program to print first N // Leyland Numbers. import java.util.*; import java.lang.*; public class GFG{ private static final int MAX = 0; // Print first n Leyland Number. public static void leyland(int n) { List<Integer> ans = new ArrayList<Integer>(); // Outer loop for x from 2 to n. for (int x = 2; x <= n; x++) { // Inner loop for y from 2 to x. for (int y = 2; y <= x; y++) { // Calculating x^y + y^x int temp = (int)Math.pow(x, y) + (int)Math.pow(y, x); ans.add(temp); } } // Sorting the all Leyland Number. Collections.sort(ans); // Printing first n Leyland number. for (int i = 0; i < n; i++) System.out.print(ans.get(i) + " "); } // Driven Program public static void main(String args[]) { int n = 6; leyland(n); } } // This code is contributed by Sachin Bisht
Python3
# Python3 program to print first N # Leyland Numbers. import math # Print first n Leyland Number. def leyland(n): ans = [] x = 2 y = 2 # Outer loop for x from 2 to n. while x <= n : # Inner loop for y from 2 to x. y = 2 while y <= x : # Calculating x^y + y^x temp = pow(x, y) + pow(y, x) ans.append(temp); y = y + 1 x = x + 1 # Sorting the all Leyland Number. ans.sort(); i = 0 # Printing first n Leyland number. while i < n : print(ans[i], end = " ") i = i + 1 # Driver Code n = 6 leyland(n) # This code is contributed by rishabh_jain
C#
// C# program to print // first N Leyland Numbers. using System; using System.Collections; class GFG { // Print first n // Leyland Number. public static void leyland(int n) { ArrayList ans = new ArrayList(); // Outer loop for x // from 2 to n. for (int x = 2; x <= n; x++) { // Inner loop for // y from 2 to x. for (int y = 2; y <= x; y++) { // Calculating x^y + y^x int temp = (int)Math.Pow(x, y) + (int)Math.Pow(y, x); ans.Add(temp); } } // Sorting the all // Leyland Number. ans.Sort(); // Printing first // n Leyland number. for (int i = 0 ; i < n; i++) { Console.Write(ans[i] + " "); } } // Driver Code public static void Main() { int n = 6; leyland(n); } } // This code is contributed by Sam007
PHP
<?php // PHP program to print // first N Leyland Numbers. $MAX = 100; // Print first n // Leyland Number. function leyland($n) { $ans; $index = 0; // Outer loop for // x from 2 to n. for ($x = 2; $x <= $n; $x++) { // Inner loop for // y from 2 to x. for ($y = 2; $y <= $x; $y++) { // Calculating x^y + y^x $temp = pow($x, $y) + pow($y, $x); $ans[$index] = $temp; $index++; } } // Sorting the all // Leyland Number. sort($ans); // Printing first // n Leyland number. for ($i = 0; $i < $n; $i++) echo $ans[$i]. " "; } // Driver Code $n = 6; leyland($n); // This code is contributed // by mits ?>
Javascript
<script> // Javascript program to print // first N Leyland Numbers. let MAX = 100; // Print first n // Leyland Number. function leyland(n) { let ans = []; let index = 0; // Outer loop for // x from 2 to n. for (let x = 2; x <= n; x++) { // Inner loop for // y from 2 to x. for (let y = 2; y <= x; y++) { // Calculating x^y + y^x let temp = Math.pow(x, y) + Math.pow(y, x); ans[index] = temp; index++; } } // Sorting the all // Leyland Number. console.log(ans) ans = ans.sort((a, b)=>a-b); console.log(ans) // Printing first // n Leyland number. for (let i = 0; i < n; i++) document.write(ans[i] + " "); } // Driver Code let n = 6; leyland(n); // This code is contributed // by _saurabh_jaiswal </script>
Producción:
8 17 32 54 57 100
Complejidad del tiempo: O(n*nlogn+nlogn)
Espacio auxiliar: O(n)