📜  利兰数

📅  最后修改于: 2021-05-06 20:04:04             🧑  作者: Mango

在数论中,莱兰数是x y + y x形式的数字,其中x和y是大于1且1 给定正整数N。任务是按升序打印第一个N Leyland编号。 Leyland的前几个数字是8、17、32、54、57、100 …
例子:

Input : N = 1
Output : 8
22 + 22 = 4 + 4 = 8.

Input : N = 6
Output : 100

运行两个循环的想法,一个用于x,另一个用于y。外循环从2到n开始,对于外循环的每次迭代,从2 t x开始运行内循环。并将x y + y x存储在一个数组中。计算完所有值后,对它们进行排序并打印前n个数字。
以下是此方法的实现:

C++
// CPP program to print first N Leyland Numbers.
#include 
#define MAX 100
using namespace std;
 
// Print first n Leyland Number.
void leyland(int n)
{
    vector 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 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.
        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


Javascript


输出:

8 17 32 54 57 100