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++
// CPP program to print first N Leyland Numbers.#include <bits/stdc++.h>#define MAX 100using 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 Programint 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 Coden = 6leyland(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 Codelet n = 6;leyland(n); // This code is contributed// by _saurabh_jaiswal</script> |
Output:
8 17 32 54 57 100
Time complexity: O(n*nlogn+nlogn)
Auxiliary space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


