Print first N terms of series (0.25, 0.5, 0.75, …) in fraction representation
Last Updated :
11 Aug, 2022
Given an integer N, the task is to print the first N terms of the series in their fraction form i.e.
1/4, 1/2, 3/4, 1, 5/4, …
The above series has values as 0.25, 0.5, 0.75, 1, 1.25, ….etc. It is an Arithmetic progression that begins with 0.25 and has a difference of 0.25.
Examples:
Input: N = 6
Output: 1/4 1/2 3/4 1 5/4 3/2
Input: N = 9
Output: 1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4
Approach: Consider the first four terms of the series as the base terms. Store the numerator elements and denominator elements separately.
Consider the first term 1/4, the fifth term is 1 + (1 * 4) / 4 which is 1/5.
Similarly, consider the second term 1/2 the sixth term is 1 + (1 * 2) / 2 which is 3/2.
Hence, we can consider the denominators will always be either 2, 4 or no denominator and the numerator of the term can be calculated from the denominator.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printSeries( int n)
{
int nmtr[4] = { 1, 1, 1, 3 };
int dntr[4] = { 0, 4, 2, 4 };
for ( int i = 1; i <= n; i++) {
if (i % 4 == 0)
cout << nmtr[i % 4] + (i / 4) - 1 << " " ;
else {
cout << nmtr[i % 4] + ((i / 4) * dntr[i % 4])
<< "/" << dntr[i % 4] << " " ;
}
}
}
int main()
{
int n = 9;
printSeries(n);
return 0;
}
|
Java
class GFG
{
public static void printSeries( int n)
{
int [] nmtr = new int []{ 1 , 1 , 1 , 3 };
int [] dntr = new int []{ 0 , 4 , 2 , 4 };
for ( int i = 1 ; i <= n; i++)
{
if (i % 4 == 0 )
System.out.print( nmtr[i % 4 ] + (i / 4 ) - 1 + " " );
else
{
System.out.print( nmtr[i % 4 ] + ((i / 4 ) * dntr[i % 4 ])
+ "/" + dntr[i % 4 ] + " " );
}
}
}
public static void main(String[] args)
{
int n = 9 ;
printSeries(n);
}
}
|
Python3
def printSeries(n):
nmtr = [ 1 , 1 , 1 , 3 ]
dntr = [ 0 , 4 , 2 , 4 ]
for i in range ( 1 , n + 1 , 1 ):
if (i % 4 = = 0 ):
print (nmtr[i % 4 ] + int (i / 4 ) - 1 ,
end = " " )
else :
print (nmtr[i % 4 ] + ( int (i / 4 ) *
dntr[i % 4 ]), end = "")
print ( "/" , end = "")
print (dntr[i % 4 ], end = " " )
if __name__ = = '__main__' :
n = 9
printSeries(n)
|
C#
using System;
class GFG
{
static void printSeries( int n)
{
int [] nmtr = { 1, 1, 1, 3 };
int [] dntr = { 0, 4, 2, 4 };
for ( int i = 1; i <= n; i++)
{
if (i % 4 == 0)
Console.Write((nmtr[i % 4] + (i / 4) - 1) + " " );
else
{
Console.Write((nmtr[i % 4] + ((i / 4) * dntr[i % 4])) +
"/" + dntr[i % 4] + " " );
}
}
}
public static void Main()
{
int n = 9;
printSeries(n);
}
}
|
Javascript
<script>
function printSeries( n)
{
let nmtr = [ 1, 1, 1, 3 ];
let dntr = [ 0, 4, 2, 4 ];
for (let i = 1; i <= n; i++)
{
if (i % 4 == 0)
document.write( nmtr[i % 4] + (i / 4) - 1 + " " );
else {
document.write( nmtr[i % 4] + (parseInt(i / 4) * dntr[i % 4])
+ "/" + dntr[i % 4] + " " );
}
}
}
let n = 9;
printSeries(n);
</script>
|
Output:
1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4
Time complexity: O(n) for given input n, because using a for loop
Auxiliary space: O(1) It is using constant space
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...