Encuentra si el número dado es la suma de los primeros n números naturales

Dado un número s (1 <= s <= 1000000000). Si este número es la suma de los primeros n números naturales, imprima, de lo contrario, imprima -1. 
Ejemplos: 

Input: s = 10
Output: n = 4
Explanation:
1 + 2 + 3 + 4 = 10

Input: s = 17
Output: n = -1
Explanation:
17 can't be expressed as a 
sum of consecutive from 1.
 

 Método 1 (Simple):

C++

// C++ program for above implementation
#include <iostream>
using namespace std;
 
// Function to find no. of elements
// to be added from 1 to get n
int findS(int s)
{
    int sum = 0;
 
    // Start adding numbers from 1
    for (int n = 1; sum < s; n++) {
        sum += n;
 
        // If sum becomes equal to s
        // return n
        if (sum == s)
            return n;
    }
 
    return -1;
}
 
// Drivers code
int main()
{
    int s = 15;
    int n = findS(s);
    n == -1 ? cout << "-1"
            : cout << n;
 
    return 0;
}

Java

// Java program for above implementation
class GFG {
     
    // Function to find no. of elements
    // to be added from 1 to get n
    static int findS(int s)
    {
        int sum = 0;
 
        // Start adding numbers from 1
        for (int n = 1; sum < s; n++)
        {
            sum += n;
     
            // If sum becomes equal to s
            // return n
            if (sum == s)
                return n;
        }
 
        return -1;
    }
 
    // Drivers code
    public static void main(String[]args)
    {
         
        int s = 15;
        int n = findS(s);
        if(n == -1)
            System.out.println("-1");
        else
            System.out.println(n);
    }
}
 
//This code is contributed by Azkia Anam.

Python3

# Python3 program to check if
# given number is sum of first n
# natural numbers
 
# Function to find no. of elements
# to be added from 1 to get n
def findS (s):
    _sum = 0
    n = 1
     
    # Start adding numbers from 1
    while(_sum < s):
        _sum += n
        n+=1
    n-=1
     
    # If sum becomes equal to s
    # return n
    if _sum == s:
        return n
    return -1
 
# Driver code
s = 15
n = findS (s)
if n == -1:
    print("-1")
else:
    print(n)
 
# This code is contributed by "Abhishek Sharma 44".

C#

// C# program for above implementation
using System;
 
class GFG {
     
    // Function to find no. of elements
    // to be added from 1 to get n
    static int findS(int s)
    {
        int sum = 0;
 
        // Start adding numbers from 1
        for (int n = 1; sum < s; n++)
        {
            sum += n;
     
            // If sum becomes equal to s
            // return n
            if (sum == s)
                return n;
        }
 
        return -1;
    }
 
    // Drivers code
    public static void Main()
    {
         
        int s = 15;
        int n = findS(s);
         
        if(n == -1)
            Console.WriteLine("-1");
        else
            Console.WriteLine(n);
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program for above implementation
 
// Function to find no. of elements
// to be added from 1 to get n
function findS($s)
{
    $sum = 0;
 
    // Start adding numbers from 1
    for ($n = 1; $sum < $s; $n++)
    {
        $sum += $n;
 
        // If sum becomes equal
        // to s return n
        if ($sum == $s)
            return $n;
    }
 
    return -1;
}
 
// Drivers code
$s = 15;
$n = findS($s);
if($n == -1)
echo "-1";
else
echo $n;
 
// This code is contributed by Sam007
?>

Javascript

<script>
 
// Javascript program for above implementation   
 
// Function to find no. of elements
// to be added from 1 to get n
    function findS(s) {
        var sum = 0;
 
        // Start adding numbers from 1
        for (n = 1; sum < s; n++) {
            sum += n;
 
            // If sum becomes equal to s
            // return n
            if (sum == s)
                return n;
        }
 
        return -1;
    }
 
    // Drivers code
     
 
        var s = 15;
        var n = findS(s);
        if (n == -1)
            document.write("-1");
        else
            document.write(n);
 
// This code is contributed by Rajput-Ji
 
</script>

C++

// C++ program for finding s such
// that sum from 1 to s equals to n
#include <iostream>
using namespace std;
 
// Function to find no. of elements
// to be added to get s
int findS(int s)
{
    int l = 1, r = (s / 2) + 1;
 
    // Apply Binary search
    while (l <= r) {
 
        // Find mid
        int mid = (l + r) / 2;
 
        // find sum of 1 to mid natural numbers
        // using formula
        int sum = mid * (mid + 1) / 2;
 
        // If sum is equal to n
        // return mid
        if (sum == s)
            return mid;
 
        // If greater than n
        // do r = mid-1
        else if (sum > s)
            r = mid - 1;
 
        // else do l = mid + 1
        else
            l = mid + 1;
    }
 
    // If not possible, return -1
    return -1;
}
 
// Drivers code
int main()
{
    int s = 15;
    int n = findS(s);
    n == -1 ? cout << "-1"
            : cout << n;
 
    return 0;
}

Java

// java program for finding s such
// that sum from 1 to s equals to n
import java.io.*;
 
public class GFG {
     
    // Function to find no. of elements
    // to be added to get s
    static int findS(int s)
    {
        int l = 1, r = (s / 2) + 1;
     
        // Apply Binary search
        while (l <= r) {
     
            // Find mid
            int mid = (l + r) / 2;
     
            // find sum of 1 to mid natural
            // numbers using formula
            int sum = mid * (mid + 1) / 2;
     
            // If sum is equal to n
            // return mid
            if (sum == s)
                return mid;
     
            // If greater than n
            // do r = mid-1
            else if (sum > s)
                r = mid - 1;
     
            // else do l = mid + 1
            else
                l = mid + 1;
        }
     
        // If not possible, return -1
        return -1;
    }
     
    // Drivers code
    static public void main (String[] args)
    {
        int s = 15;
        int n = findS(s);
         
        if(n==-1)
            System.out.println("-1");
        else
            System.out.println(n);
    }
}
 
// This code is contributed by vt_m.

Python3

# python program for finding s such
# that sum from 1 to s equals to n
 
# Function to find no. of elements
# to be added to get s
def findS(s):
     
    l = 1
    r = int(s / 2) + 1
  
    # Apply Binary search
    while (l <= r) :
        # Find mid
        mid = int((l + r) / 2)
  
        # find sum of 1 to mid natural numbers
        # using formula
        sum = int(mid * (mid + 1) / 2)
  
        # If sum is equal to n
        # return mid
        if (sum == s):
            return mid
  
        # If greater than n
        # do r = mid-1
        elif (sum > s):
            r = mid - 1
  
        # else do l = mid + 1
        else:
            l = mid + 1
     
  
    # If not possible, return -1
    return -1
 
 
s = 15
n = findS(s)
if(n == -1):
    print( "-1")
else:
    print( n )
 
# This code is contributed by Sam007

C#

// C# program for finding s such
// that sum from 1 to s equals to n
using System;
 
public class GFG {
     
    // Function to find no. of elements
    // to be added to get s
    static int findS(int s)
    {
        int l = 1, r = (s / 2) + 1;
     
        // Apply Binary search
        while (l <= r) {
     
            // Find mid
            int mid = (l + r) / 2;
     
            // find sum of 1 to mid natural
            // numbers using formula
            int sum = mid * (mid + 1) / 2;
     
            // If sum is equal to n
            // return mid
            if (sum == s)
                return mid;
     
            // If greater than n
            // do r = mid-1
            else if (sum > s)
                r = mid - 1;
     
            // else do l = mid + 1
            else
                l = mid + 1;
        }
     
        // If not possible, return -1
        return -1;
    }
     
    // Drivers code
    static public void Main ()
    {
        int s = 15;
        int n = findS(s);
         
        if(n==-1)
            Console.WriteLine("-1");
        else
            Console.WriteLine(n);
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program for finding
// s such that sum from 1
// to s equals to n
 
// Function to find no.
// of elements to be added
// to get s
function findS($s)
{
    $l = 1;
    $r = 1 + (int)$s / 2;
 
 
    // Apply Binary search
    while ($l <= $r)
    {
 
        // Find mid
        $mid = (int)(($l + $r) / 2);
 
        // find sum of 1 to mid natural
        // numbers using formula
        $sum = (int)($mid *
                    ($mid + 1) / 2);
 
        // If sum is equal to n
        // return mid
        if ($sum == $s)
            return $mid;
 
        // If greater than n
        // do r = mid-1
        else if ($sum > $s)
            $r = $mid - 1;
 
        // else do l = mid + 1
        else
            $l = $mid + 1;
    }
 
    // If not possible,
    // return -1
    return -1;
}
 
// Drivers code
$s = 15;
$n = findS($s);
if($n == -1 )
echo "-1";
else
echo $n;
 
// This code is contributed by Sam007
?>

Javascript

<script>
 
// Javascript program for finding s such
// that sum from 1 to s equals to n public
 
    // Function to find no. of elements
    // to be added to get s
    function findS(s) {
        var l = 1, r = parseInt((s / 2) + 1);
 
        // Apply Binary search
        while (l <= r) {
 
            // Find mid
            var mid = parseInt( (l + r) / 2);
 
            // find sum of 1 to mid natural
            // numbers using formula
            var sum = mid * parseInt((mid + 1) / 2);
 
            // If sum is equal to n
            // return mid
            if (sum == s)
                return mid;
 
            // If greater than n
            // do r = mid-1
            else if (sum > s)
                r = mid - 1;
 
            // else do l = mid + 1
            else
                l = mid + 1;
        }
 
        // If not possible, return -1
        return -1;
    }
 
    // Drivers code
        var s = 15;
        var n = findS(s);
 
        if (n == -1)
            document.write("-1");
        else
            document.write(n);
 
// This code contributed by Rajput-Ji
 
</script>

C++

// C++ program of the above
// approach
 
#include <bits/stdc++.h>
 
# define ll long long
 
using namespace std;
 
// Function to check if the
// s is the sum of first N
// natural number
ll int isvalid(ll int s)
{
    // Solution of Quadratic Equation
    float k=(-1+sqrt(1+8*s))/2;
   
    // Condition to check if the
    // solution is a integer
    if(ceil(k)==floor(k))
        return k;
    else
      return -1;
}
 
// Driver Code
int main()
{
    int s = 15;
   
    // Function Call
    cout<< isvalid(s);
    return 0;
}

Java

// Java program of the above
// approach
import java.util.*;
 
class GFG{
     
// Function to check if the
// s is the sum of first N
// natural number
public static int isvalid(int s)
{
     
    // Solution of Quadratic Equation
    double k = (-1.0 + Math.sqrt(1 + 8 * s)) / 2;
    
    // Condition to check if the
    // solution is a integer
    if (Math.ceil(k) == Math.floor(k))
        return (int)k;
    else
      return -1;
}
     
// Driver code
public static void main(String[] args)
{
    int s = 15;
 
    // Function call
    System.out.print(isvalid(s));
}
}
 
// This code is contributed by divyeshrabadiya07

Python3

# Python3 program of the above
# approach
import math
 
# Function to check if the
# s is the sum of first N
# natural number
def isvalid(s):
     
    # Solution of Quadratic Equation
    k = (-1 + math.sqrt(1 + 8 * s)) / 2
 
    # Condition to check if the
    # solution is a integer
    if (math.ceil(k) == math.floor(k)):
        return int(k)
    else:
        return -1
 
# Driver Code
s = 15
 
# Function Call
print(isvalid(s))
 
# This code is contributed by vishu2908

C#

// C# program of the above
// approach
using System;
class GFG{
     
// Function to check if the
// s is the sum of first N
// natural number
public static int isvalid(int s)
{
  // Solution of Quadratic Equation
  double k = (-1.0 + Math.Sqrt
             (1 + 8 * s)) / 2;
 
  // Condition to check if the
  // solution is a integer
  if (Math.Ceiling(k) ==
      Math.Floor(k))
    return (int)k;
  else
    return -1;
}
 
// Driver code
public static void Main(string[] args)
{
  int s = 15;
 
  // Function call
  Console.Write(isvalid(s));
}
}
 
// This code is contributed by Chitranayal

Javascript

<script>
 
// Javascript program of the above
// approach
 
// Function to check if the
// s is the sum of first N
// natural number
function isvalid(s)
{
      
    // Solution of Quadratic Equation
    let k = (-1.0 + Math.sqrt(1 + 8 * s)) / 2;
     
    // Condition to check if the
    // solution is a integer
    if (Math.ceil(k) == Math.floor(k))
        return k;
    else
      return -1;
}
// Driver code
 
        let s = 15;
  
    // Function call
    document.write(isvalid(s));
           
</script>

Publicación traducida automáticamente

Artículo escrito por Sahil_Chhabra 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 *