Dado un número N, escribe una función para expresar N como la suma de dos o más números positivos consecutivos. Si no hay solución, salida -1. Si hay varias soluciones, imprima una de ellas.
Ejemplos:
Input : N = 10 Output : 4 + 3 + 2 + 1 Input : N = 8 Output : -1 Input : N = 24 Output : 9 + 8 + 7
Sum of first n natural numbers = n * (n + 1)/2 Sum of first (n + k) numbers = (n + k) * (n + k + 1)/2 If N is sum of k consecutive numbers, then following must be true. N = [(n+k)(n+k+1) - n(n+1)] / 2 OR 2 * N = [(n+k)(n+k+1) - n(n+1)]
A continuación se muestra la implementación basada en la idea anterior.
C++
// C++ program to print a consecutive sequence // to express N if possible. #include <bits/stdc++.h> using namespace std; // Print consecutive numbers from // last to first void printConsecutive(int last, int first) { cout << first++; for (int x = first; x<= last; x++) cout << " + " << x; } void findConsecutive(int N) { for (int last=1; last<N; last++) { for (int first=0; first<last; first++) { if (2*N == (last-first)*(last+first+1)) { cout << N << " = "; printConsecutive(last, first+1); return; } } } cout << "-1"; } // Driver code int main() { int n = 12; findConsecutive(n); return 0; }
Java
// Java program to print a consecutive sequence // to express N if possible. import java.util.*; class GFG { // Print consecutive numbers from // last to first static void printConsecutive(int last, int first) { System.out.print(first++); for (int x = first; x<= last; x++) System.out.print(" + " + x); } static void findConsecutive(int N) { for (int last = 1; last < N; last++) { for (int first = 0; first < last; first++) { if (2*N == (last-first)*(last+first+1)) { System.out.print(N+ " = "); printConsecutive(last, first+1); return; } } } System.out.print("-1"); } // Driver code public static void main(String[] args) { int n = 12; findConsecutive(n); } } // This code is contributed by umadevi9616
Python3
# Python3 program to print a consecutive # sequence to express N if possible. # Print consecutive numbers # from last to first def printConsecutive(last, first): print (first, end = "") first += 1 for x in range(first, last + 1): print (" +", x, end = "") def findConsecutive(N): for last in range(1, N): for first in range(0, last): if 2 * N == (last - first) * (last + first + 1): print (N, "= ", end = "") printConsecutive(last, first + 1) return print ("-1") # Driver code n = 12 findConsecutive(n) # This code is contributed by Shreyanshi Arun.
C#
// C# program to print a consecutive sequence // to express N if possible. using System; class GfG { // Print consecutive numbers from // last to first static void printConsecutive(int last, int first) { Console.Write(first++); for (int x = first; x <= last; x++) Console.Write(" + "+x); } static void findConsecutive(int N) { for (int last = 1; last < N; last++) { for (int first = 0; first < last; first++) { if (2 * N == (last - first) * (last + first + 1)) { Console.Write(N + " = "); printConsecutive(last, first + 1); return; } } } Console.Write("-1"); } // Driver code public static void Main () { int n = 12; findConsecutive(n); } } // This code is contributed by vt_m
PHP
<?php // PHP program to print a consecutive // sequence to express N if possible. // Print consecutive numbers from // last to first function printConsecutive($last, $first) { echo $first++; for ($x = $first; $x<= $last; $x++) echo " + " , $x; } function findConsecutive($N) { for ($last = 1; $last < $N; $last++) { for ($first = 0; $first < $last; $first++) { if (2 * $N == ($last - $first) * ($last + $first + 1)) { echo $N , " = "; printConsecutive($last, $first + 1); return; } } } echo "-1"; } // Driver Code $n = 12; findConsecutive($n); // This code is contributed by nitin mittal ?>
Javascript
<script> // Javascript program to print a consecutive // sequence to express N if possible. // Print consecutive numbers from // last to first function printConsecutive(last, first) { document.write(first++); for (let x = first; x<= last; x++) document.write( " + " + x); } function findConsecutive(N) { for (let last = 1; last < N; last++) { for (let first = 0; first < last; first++) { if (2 * N == (last - first) * (last + first + 1)) { document.write(N + " = "); printConsecutive(last, first + 1); return; } } } document.write("-1"); } // Driver Code let n = 12; findConsecutive(n); // This code is contributed by _saurabh_jaiswal </script>
Producción:
12 = 3 + 4 + 5
Referencia:
https://math.stackexchange.com/questions/139842/in-how-many-ways-can-a-number-be-expressed-as-a-sum-of-consecutive-numbers
Este artículo es una contribución de Roshni Agarval . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA