Imprime los primeros N términos de la serie (0.25, 0.5, 0.75, …) en representación fraccionaria

Dado un número entero N , la tarea es imprimir los primeros N términos de la serie en su forma de fracción, es decir 
 

1/4, 1/2, 3/4, 1, 5/4, …

La serie anterior tiene valores como 0.25, 0.5, 0.75, 1, 1.25,….etc. Es una progresión aritmética que comienza en 0,25 y tiene una diferencia de 0,25.
Ejemplos: 
 

Entrada: N = 6 
Salida: 1/4 1/2 3/4 1 5/4 3/2
Entrada: N = 9 
Salida: 1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4 
 

Enfoque: Considere los primeros cuatro términos de la serie como términos base. Almacene los elementos del numerador y los elementos del denominador por separado. 
Considere el primer término 1/4 , el quinto término es 1 + (1 * 4) / 4 que es 1/5
Del mismo modo, considere el segundo término 1/2 , el sexto término es 1 + (1 * 2) / 2, que es 3/2
Por lo tanto, podemos considerar que los denominadores siempre serán 2 , 4 o ningún denominador y el numerador del término se puede calcular a partir del denominador.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

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

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

Python3

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

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

Javascript

<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>
Producción: 

1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4

 

Publicación traducida automáticamente

Artículo escrito por SanjayR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *