Leyland Number
Last Updated :
17 Jul, 2022
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++
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
void leyland( int n)
{
vector< int > ans;
for ( int x = 2; x <= n; x++) {
for ( int y = 2; y <= x; y++) {
int temp = pow (x, y) + pow (y, x);
ans.push_back(temp);
}
}
sort(ans.begin(), ans.end());
for ( int i = 0; i < n; i++)
cout << ans[i] << " " ;
}
int main()
{
int n = 6;
leyland(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG{
private static final int MAX = 0 ;
public static void leyland( int n)
{
List<Integer> ans = new ArrayList<Integer>();
for ( int x = 2 ; x <= n; x++) {
for ( int y = 2 ; y <= x; y++) {
int temp = ( int )Math.pow(x, y) +
( int )Math.pow(y, x);
ans.add(temp);
}
}
Collections.sort(ans);
for ( int i = 0 ; i < n; i++)
System.out.print(ans.get(i) + " " );
}
public static void main(String args[])
{
int n = 6 ;
leyland(n);
}
}
|
Python3
import math
def leyland(n):
ans = []
x = 2
y = 2
while x < = n :
y = 2
while y < = x :
temp = pow (x, y) + pow (y, x)
ans.append(temp);
y = y + 1
x = x + 1
ans.sort();
i = 0
while i < n :
print (ans[i], end = " " )
i = i + 1
n = 6
leyland(n)
|
C#
using System;
using System.Collections;
class GFG
{
public static void leyland( int n)
{
ArrayList ans = new ArrayList();
for ( int x = 2; x <= n; x++)
{
for ( int y = 2; y <= x; y++)
{
int temp = ( int )Math.Pow(x, y) +
( int )Math.Pow(y, x);
ans.Add(temp);
}
}
ans.Sort();
for ( int i = 0 ; i < n; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main()
{
int n = 6;
leyland(n);
}
}
|
PHP
<?php
$MAX = 100;
function leyland( $n )
{
$ans ;
$index = 0;
for ( $x = 2; $x <= $n ; $x ++)
{
for ( $y = 2; $y <= $x ; $y ++)
{
$temp = pow( $x , $y ) +
pow( $y , $x );
$ans [ $index ] = $temp ;
$index ++;
}
}
sort( $ans );
for ( $i = 0; $i < $n ; $i ++)
echo $ans [ $i ]. " " ;
}
$n = 6;
leyland( $n );
?>
|
Javascript
<script>
let MAX = 100;
function leyland(n)
{
let ans = [];
let index = 0;
for (let x = 2; x <= n; x++)
{
for (let y = 2; y <= x; y++)
{
let temp = Math.pow(x, y) +
Math.pow(y, x);
ans[index] = temp;
index++;
}
}
console.log(ans)
ans = ans.sort((a, b)=>a-b);
console.log(ans)
for (let i = 0; i < n; i++)
document.write(ans[i] + " " );
}
let n = 6;
leyland(n);
</script>
|
Output:
8 17 32 54 57 100
Time complexity: O(n*nlogn+nlogn)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...