Thursday, October 9, 2025

Beatty sequence

Beatty sequence (or homogeneous Beatty sequence) is the sequence of integers found by taking the floor of the positive multiples of a positive irrational number.
The Nth term of the Beatty sequence: 
 

T(i) = \lfloor N * \sqrt{2} \rfloor
 

 

Find the N terms of Beatty Sequence

Given an integer N, the task is to print the first N terms of the Beatty sequence.
Examples: 
 

Input: N = 5 
Output: 1, 2, 4, 5, 7
Input: N = 10 
Output: 1, 2, 4, 5, 7, 8, 9, 11, 12, 
 

 

Approach: The idea is to iterate from 1 to N using loop to find the i^{th}     term of the sequence. The i^{th}     term of the Beatty sequence is given by:
 

T(i) = \lfloor i * \sqrt{2} \rfloor
 

Below is the implementation of the above approach: 
 

C++




// C++ implementation of the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the first N terms
// of the Beatty sequence
void BeattySequence(int n)
{
    for (int i = 1; i <= n; i++) {
        double ans = floor(i * sqrt(2));
        cout << ans << ", ";
    }
}
 
// Driver code
int main()
{
    int n = 5;
 
    BeattySequence(n);
 
    return 0;
}


Java




// Java implementation of the
// above approach
import java.util.*;
class GFG{
 
// Function to print the first N terms
// of the Beatty sequence
static void BeattySequence(int n)
{
    for(int i = 1; i <= n; i++)
    {
        int ans = (int)Math.floor(i * Math.sqrt(2));
        System.out.print(ans + ", ");
    }
}
 
// Driver code
public static void main(String args[])
{
    int n = 5;
 
    BeattySequence(n);
}
}
 
// This code is contributed by Code_Mech


Python3




# Python3 implementation of the
# above approach
import math
 
# Function to print the first N terms
# of the Beatty sequence
def BeattySequence(n):
    for i in range(1, n + 1):
        ans = math.floor(i * math.sqrt(2))
        print(ans, end = ', ')
 
# Driver code
n = 5
BeattySequence(n)
 
# This code is contributed by yatin


C#




// C# implementation of the
// above approach
using System;
class GFG{
 
// Function to print the first N terms
// of the Beatty sequence
static void BeattySequence(int n)
{
    for(int i = 1; i <= n; i++)
    {
       double ans = Math.Floor(i * Math.Sqrt(2));
       Console.Write(ans + ", ");
    }
}
 
// Driver code
public static void Main()
{
    int n = 5;
 
    BeattySequence(n);
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
// Javascript implementation of the
// above approach
 
    // Function to print the first N terms
    // of the Beatty sequence
    function BeattySequence( n) {
        for ( let i = 1; i <= n; i++) {
            let ans = parseInt( Math.floor(i * Math.sqrt(2)));
            document.write(ans + ", ");
        }
    }
 
    // Driver code
      
        let n = 5;
 
        BeattySequence(n);
// This code contributed by Rajput-Ji
</script>


Output: 

1, 2, 4, 5, 7,

 

Time Complexity: O(n1/2)

Auxiliary Space: O(1)

Reference: https://oeis.org/A001951
 

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 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS