Un número triangular o número triangular cuenta objetos dispuestos en un triángulo equilátero, como en el diagrama de la derecha. El n-ésimo número triangular es el número de puntos que componen un triángulo con n puntos en un lado, y es igual a la suma de los n números naturales de 1 a n.
Ejemplos:
Input : 5 Output : 1 3 6 10 15 Input : 10 Output : 1 3 6 10 15 21 28 36 45 55 Explanation : For k = 1 and j = 1 -> print k ( i.e. 1); increase j by 1 and add into k then print k ( i.e 3 ) update k increase j by 1 and add into k then print k ( i.e 6 ) update k increase j by 1 and add into k then print k ( i.e 10 ) update k increase j by 1 and add into k then print k ( i.e 15 ) update k increase j by 1 and add into k then print k ( i.e 21 ) update k . . and so on.
El enfoque utilizado es muy simple. Iterar el ciclo for hasta el valor dado n y para cada iteración aumentar j en 1 y agregarlo a k, lo que simplemente imprimirá la serie de números triangulares hasta n.
A continuación se muestra el programa que implementa el enfoque anterior:
C
// C Program to find Triangular Number Series #include <stdio.h> // Function to find triangular number void triangular_series(int n) { int i, j = 1, k = 1; // For each iteration increase j by 1 // and add it into k for (i = 1; i <= n; i++) { printf(" %d ", k); j = j + 1; // Increasing j by 1 k = k + j; // Add value of j into k and update k } } // Driven Function int main() { int n = 5; triangular_series(n); return 0; }
Java
// Java Program to print triangular number series till n import java.util.*; class GFG { // Function to find triangular number static void triangular_series(int n) { int i, j = 1, k = 1; // For each iteration increase j by 1 // and add it into k for (i = 1; i <= n; i++) { System.out.printf("%d ", k); j = j + 1; // Increasing j by 1 k = k + j; // Add value of j into k and update k } } // Driver function public static void main(String[] args) { int n = 5; triangular_series(n); } } // This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to find Triangular # Number Series # Function to find triangular number def triangular_series( n ): j = 1 k = 1 # For each iteration increase j # by 1 and add it into k for i in range(1, n + 1): print(k, end = ' ') j = j + 1 # Increasing j by 1 # Add value of j into k and update k k = k + j # Driven Code n = 5 triangular_series(n) # This code is contributed by "Sharad_Bhardwaj"
C#
// C# Program to print triangular // number series till n using System; class GFG { // Function to find triangular number static void triangular_series(int n) { int i, j = 1, k = 1; // For each iteration increase j by 1 // and add it into k for (i = 1; i <= n; i++) { Console.Write(k +" "); j += 1; // Increasing j by 1 k += j; // Add value of j into k and update k } } // Driver Code public static void Main() { int n = 5; triangular_series(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to find // Triangular Number Series // Function to find // triangular number function triangular_series($n) { $i; $j = 1; $k = 1; // For each iteration increase j // by 1 and add it into k for ($i = 1; $i <= $n; $i++) { echo(" " . $k . " "); // Increasing j by 1 $j = $j + 1; // Add value of j into k and update k $k = $k + $j; } } // Driver Code $n = 5; triangular_series($n); // This code is contributed by Ajit. ?>
Javascript
<script> // javascript Program to find Triangular Number Series // Function to find triangular number function triangular_series( n) { let i, j = 1, k = 1; // For each iteration increase j by 1 // and add it into k for (i = 1; i <= n; i++) { document.write(k+" "); j = j + 1; // Increasing j by 1 k = k + j; // Add value of j into k and update k } } // Driven Function let n = 5; triangular_series(n); // This code is contributed by Rajput-Ji </script>
Producción :
1 3 6 10 15
Complejidad del tiempo : O(n)
Espacio auxiliar : O(1)
Solución alternativa:
La solución se basa en el hecho de que el i-ésimo número triangular es la suma de los primeros i números naturales, es decir, i * (i + 1)/2
C
// C Program to find Triangular Number Series #include <stdio.h> // Function to find triangular number void triangular_series(int n) { for (int i = 1; i <= n; i++) printf(" %d ", i*(i+1)/2); } // Driven Function int main() { int n = 5; triangular_series(n); return 0; }
Java
//Java program to print triangular number series till n import java.util.*; class GFG { // Function to find triangular number static void triangular_series(int n) { for (int i = 1; i <= n; i++) System.out.printf("%d ";, i*(i+1)/2); } // Driver function public static void main(String[] args) { int n = 5; triangular_series(n); } } //This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to find Triangular # Number Series def triangular_series(n): for i in range(1, n + 1): print( i*(i+1)//2,end=' ') # Driver code n = 5 triangular_series(n) # This code is contributed by ihritik
C#
// C# program to print triangular // number series till n using System; class GFG { // Function to find triangular number static void triangular_series(int n) { for (int i = 1; i <= n; i++) Console.Write(i * (i + 1) / 2 + " "); } // Driver Code public static void Main() { int n = 5; triangular_series(n); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program to find // Triangular Number Series // Function to find // triangular number function triangular_series($n) { for ($i = 1; $i <= $n; $i++) echo(" " . $i * ($i + 1) / 2 . " "); } // Driver Code $n = 5; triangular_series($n); // This code is contributed by Ajit. ?>
Javascript
<script> // javascript Program to find Triangular Number Series // Function to find triangular number function triangular_series( n) { for (let i = 1; i <= n; i++) document.write(" "+ i * (i + 1)/2); } // Driven Function let n = 5; triangular_series(n); // This code is contributed by gauravrajput1 </script>
Producción :
1 3 6 10 15
Complejidad de tiempo: O(n)
Espacio auxiliar: O(1) , ya que no se ha tomado ningún espacio adicional.
Publicación traducida automáticamente
Artículo escrito por Shahnawaz_Ali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA