Leyland Number

In number theory, a Leyland number is a number of the form xy + yx, where x and y are integers greater than 1 and 1 <y <= x.
Given a positive integer N. The task is to print first N Leyland number in ascending order. The first few Leyland numbers are 8, 17, 32, 54, 57, 100, …

Examples:

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

Input : N = 6
Output : 100



The idea to run two loop, one for x and other for y. The outer loop start with 2 to n and for each iteration of outer loop, run inner loop start from 2 t x. And store xy + yx in an array. After calculating all the value sort them and print first n numbers.

Below is the implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



Output:

8 17 32 54 57 100


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sam007, Mithun Kumar