Alcuin’s Sequence
Last Updated :
28 May, 2022
Alcuin sequence is the expansion of
This series has significant importance as
- Alcuin Sequence a(n) is the number of triangles with integer sides and the perimeter of the triangle is n.
- Alcuin Sequence a(n) is the number of triangles with distinct integer sides and the perimeter of the triangle is n+6.
The Alcuin sequence is as follows:
0, 0, 1, 0, 1, 1, 2, 1, 3, 2
Examples:
Input: n = 10
Output: 0, 0, 1, 0, 1,
1, 2, 1, 3, 2
Input: n = 15
Output:0, 0, 1, 0, 1,
1, 2, 1, 3, 2,
4, 3, 5, 4, 7,
Approach:
- Find the nth term of the Alcuin sequence using
a(n) = round(n^2/12) – floor(n/4)*floor((n+2)/4)
- Find out the ith term and display it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Alcuin( int n)
{
double _n = n, ans;
ans = round((_n * _n) / 12)
- floor (_n / 4)
* floor ((_n + 2) / 4);
return ans;
}
void solve( int n)
{
int i = 0;
for ( int i = 1; i <= n; i++) {
cout << Alcuin(i) << ", " ;
}
}
int main()
{
int n = 15;
solve(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Alcuin( int n)
{
double _n = n, ans;
ans = Math.round((_n * _n) / 12 ) -
Math.floor(_n / 4 ) *
Math.floor((_n + 2 ) / 4 );
return ( int ) ans;
}
static void solve( int n)
{
int i = 0 ;
for (i = 1 ; i <= n; i++)
{
System.out.print(Alcuin(i) + ", " );
}
}
public static void main(String[] args)
{
int n = 15 ;
solve(n);
}
}
|
Python3
from math import ceil, floor
def Alcuin(n):
_n = n
ans = 0
ans = ( round ((_n * _n) / 12 ) -
floor(_n / 4 ) *
floor((_n + 2 ) / 4 ))
return ans
def solve(n):
for i in range ( 1 , n + 1 ):
print (Alcuin(i), end = ", " )
n = 15
solve(n)
|
C#
using System;
class GFG
{
static int Alcuin( int n)
{
double _n = n, ans;
ans = Math.Round((_n * _n) / 12) -
Math.Floor(_n / 4) *
Math.Floor((_n + 2) / 4);
return ( int ) ans;
}
static void solve( int n)
{
int i = 0;
for (i = 1; i <= n; i++)
{
Console.Write(Alcuin(i) + ", " );
}
}
public static void Main(String[] args)
{
int n = 15;
solve(n);
}
}
|
Javascript
<script>
function Alcuin(n)
{
let _n = n, ans;
ans = Math.round((_n * _n) / 12)
- Math.floor(_n / 4)
* Math.floor((_n + 2) / 4);
return ans;
}
function solve(n)
{
let i = 0;
for (let i = 1; i <= n; i++) {
document.write(Alcuin(i) + ", " );
}
}
let n = 15;
solve(n);
</script>
|
Output:
0, 0, 1, 0, 1, 1, 2, 1, 3, 2, 4, 3, 5, 4, 7,
Time Complexity: O(n), since the loop runs once from 1 to n.
Auxiliary Space: O(1), since extra space has not been taken.
Reference: https://en.wikipedia.org/wiki/Alcuin%27s_sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...