Print first N terms of Lower Wythoff sequence
Last Updated :
31 May, 2022
Given an integer N, the task is to print the first N terms of the Lower Wythoff sequence.
Examples:
Input: N = 5
Output: 1, 3, 4, 6, 8
Input: N = 10
Output: 1, 3, 4, 6, 8, 9, 11, 12, 14, 16
Approach: Lower Wythoff sequence is a sequence whose nth term is a(n) = floor(n * phi) where phi = (1 + sqrt(5)) / 2. So, we run a loop and find the first n terms of the sequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void lowerWythoff( int n)
{
double phi = (1 + sqrt (5)) / 2.0;
for ( int i = 1; i <= n; i++) {
double ans = floor (i * phi);
cout << ans;
if (i != n)
cout << ", " ;
}
}
int main()
{
int n = 5;
lowerWythoff(n);
return 0;
}
|
Java
class GFG
{
static void lowerWythoff( int n)
{
double phi = ( 1 + Math.sqrt( 5 )) / 2.0 ;
for ( int i = 1 ; i <= n; i++)
{
double ans = Math.floor(i * phi);
System.out.print(( int )ans);
if (i != n)
System.out.print( " , " );
}
}
public static void main(String[] args)
{
int n = 5 ;
lowerWythoff(n);
}
}
|
Python3
from math import sqrt, floor
def lowerWythoff(n) :
phi = ( 1 + sqrt( 5 )) / 2 ;
for i in range ( 1 , n + 1 ) :
ans = floor(i * phi);
print (ans,end = "");
if (i ! = n) :
print ( ", " ,end = "");
if __name__ = = "__main__" :
n = 5 ;
lowerWythoff(n);
|
C#
using System;
class GFG
{
static void lowerWythoff( int n)
{
double phi = (1 + Math.Sqrt(5)) / 2.0;
for ( int i = 1; i <= n; i++)
{
double ans = Math.Floor(i * phi);
Console.Write(( int )ans);
if (i != n)
Console.Write( " , " );
}
}
static public void Main ()
{
int n = 5;
lowerWythoff(n);
}
}
|
Javascript
<script>
function lowerWythoff(n)
{
var phi = (1 + Math.sqrt(5)) / 2.0;
for ( var i = 1; i <= n; i++)
{
var ans = Math.floor(i * phi);
document.write( ans);
if (i != n)
document.write( ", " );
}
}
var n = 5;
lowerWythoff(n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...