Print first N terms of Lower Wythoff sequence

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the first n terms
// of the lower Wythoff sequence
void lowerWythoff(int n)
{
  
    // Caluclate value of phi
    double phi = (1 + sqrt(5)) / 2.0;
  
    // Find the numbers
    for (int i = 1; i <= n; i++) {
  
        // a(n) = floor(n * phi)
        double ans = floor(i * phi);
  
        // Print the nth numbers
        cout << ans;
  
        if (i != n)
            cout << ", ";
    }
}
  
// Driver code
int main()
{
    int n = 5;
  
    lowerWythoff(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to print the first n terms
// of the lower Wythoff sequence
static void lowerWythoff(int n)
{
  
    // Caluclate value of phi
    double phi = (1 + Math.sqrt(5)) / 2.0;
  
    // Find the numbers
    for (int i = 1; i <= n; i++) 
    {
  
        // a(n) = floor(n * phi)
        double ans = Math.floor(i * phi);
  
        // Print the nth numbers
        System.out.print((int)ans);
  
        if (i != n)
            System.out.print(" , ");
    }
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 5;
  
    lowerWythoff(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# from math import sqrt,floor
from math import sqrt, floor
  
# Function to print the first n terms 
# of the lower Wythoff sequence 
def lowerWythoff(n) : 
  
    # Caluclate value of phi 
    phi = (1 + sqrt(5)) / 2
  
    # Find the numbers 
    for i in range(1, n + 1) :
  
        # a(n) = floor(n * phi) 
        ans = floor(i * phi); 
  
        # Print the nth numbers 
        print(ans,end=""); 
  
        if (i != n) :
            print( ", ",end = ""); 
  
# Driver code 
if __name__ == "__main__"
  
    n = 5
    lowerWythoff(n);
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to print the first n terms
// of the lower Wythoff sequence
static void lowerWythoff(int n)
{
  
    // Caluclate value of phi
    double phi = (1 + Math.Sqrt(5)) / 2.0;
  
    // Find the numbers
    for (int i = 1; i <= n; i++) 
    {
  
        // a(n) = floor(n * phi)
        double ans = Math.Floor(i * phi);
  
        // Print the nth numbers
        Console.Write((int)ans);
  
        if (i != n)
            Console.Write(" , ");
    }
}
  
// Driver code
static public void Main ()
{
    int n = 5;
  
    lowerWythoff(n);
}
}
  
// This code is contributed by ajit.

chevron_right


Output:

1, 3, 4, 6, 8


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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 : 29AjayKumar, jit_t, AnkitRai01