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++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to print the required series void printSeries( int n)
{ // Numerators for the first four numerators
// of the series
int nmtr[4] = { 1, 1, 1, 3 };
// Denominators for the first four denominators
// of the series
int dntr[4] = { 0, 4, 2, 4 };
for ( int i = 1; i <= n; i++) {
// If location of the term in the series is
// a multiple of 4 then there will be no denominator
if (i % 4 == 0)
cout << nmtr[i % 4] + (i / 4) - 1 << " " ;
// Otherwise there will be denominator
else {
// Printing the numerator and the denominator terms
cout << nmtr[i % 4] + ((i / 4) * dntr[i % 4])
<< "/" << dntr[i % 4] << " " ;
}
}
} // Driver code int main()
{ int n = 9;
printSeries(n);
return 0;
} |
// Java implementation of the approach class GFG
{ // Function to print the required series public static void printSeries( int n)
{ // Numerators for the first four numerators
// of the series
int [] nmtr = new int []{ 1 , 1 , 1 , 3 };
// Denominators for the first four denominators
// of the series
int [] dntr = new int []{ 0 , 4 , 2 , 4 };
for ( int i = 1 ; i <= n; i++)
{
// If location of the term in the series is
// a multiple of 4 then there will be no denominator
if (i % 4 == 0 )
System.out.print( nmtr[i % 4 ] + (i / 4 ) - 1 + " " );
// Otherwise there will be denominator
else
{
// Printing the numerator and the denominator terms
System.out.print( nmtr[i % 4 ] + ((i / 4 ) * dntr[i % 4 ])
+ "/" + dntr[i % 4 ] + " " );
}
}
} // Driver code public static void main(String[] args)
{ int n = 9 ;
printSeries(n);
} } // This code is contributed // by 29AjayKumar |
# Python 3 implementation of the approach # Function to print the required series def printSeries(n):
# Numerators for the first four
# numerators of the series
nmtr = [ 1 , 1 , 1 , 3 ]
# Denominators for the first four
# denominators of the series
dntr = [ 0 , 4 , 2 , 4 ]
for i in range ( 1 , n + 1 , 1 ):
# If location of the term in the
# series is a multiple of 4 then
# there will be no denominator
if (i % 4 = = 0 ):
print (nmtr[i % 4 ] + int (i / 4 ) - 1 ,
end = " " )
# Otherwise there will be denominator
else :
# Printing the numerator and
# the denominator terms
print (nmtr[i % 4 ] + ( int (i / 4 ) *
dntr[i % 4 ]), end = "")
print ( "/" , end = "")
print (dntr[i % 4 ], end = " " )
# Driver code if __name__ = = '__main__' :
n = 9
printSeries(n)
# This code is contributed by # Shashank_Sharma |
// C# implementation of the approach using System;
class GFG
{ // Function to print the required series static void printSeries( int n)
{ // Numerators for the first four numerators
// of the series
int [] nmtr = { 1, 1, 1, 3 };
// Denominators for the first four denominators
// of the series
int [] dntr = { 0, 4, 2, 4 };
for ( int i = 1; i <= n; i++)
{
// If location of the term in the series is
// a multiple of 4 then there will be no denominator
if (i % 4 == 0)
Console.Write((nmtr[i % 4] + (i / 4) - 1) + " " );
// Otherwise there will be denominator
else
{
// Printing the numerator and the denominator terms
Console.Write((nmtr[i % 4] + ((i / 4) * dntr[i % 4])) +
"/" + dntr[i % 4] + " " );
}
}
} // Driver code public static void Main()
{ int n = 9;
printSeries(n);
} } // This code is contributed // by Akanksha Rai |
<script> // javascript implementation of the approach // Function to print the required series function printSeries( n)
{ // Numerators for the first four numerators
// of the series
let nmtr = [ 1, 1, 1, 3 ];
// Denominators for the first four denominators
// of the series
let dntr = [ 0, 4, 2, 4 ];
for (let i = 1; i <= n; i++)
{
// If location of the term in the series is
// a multiple of 4 then there will be no denominator
if (i % 4 == 0)
document.write( nmtr[i % 4] + (i / 4) - 1 + " " );
// Otherwise there will be denominator
else {
// Printing the numerator and the denominator terms
document.write( nmtr[i % 4] + (parseInt(i / 4) * dntr[i % 4])
+ "/" + dntr[i % 4] + " " );
}
}
} // Driver code let n = 9;
printSeries(n);
// This code is contributed by Rajput-Ji </script> |
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