números asombrosos

Número asombroso es un número N cuya representación se puede descomponer en dos partes, a y b , de modo que N es igual a la suma de los números enteros de a a b y a + b = N donde ‘+’ denota concatenación.
Algunos números asombrosos son: 
 

15, 27, 429, 1353, 1863, 3388, 3591, 7119.. 
 

Comprueba si N es un número asombroso

Dado un número N , la tarea es verificar si N es un número asombroso o no. Si N es un número asombroso, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos: 
 

Entrada: N = 429 
Salida: Sí 
Explicación: 
429 = 4 + 5 + 6 …….. + 29, donde a = 4, b = 29 
y a + b = 429 donde + denota concatenación
Entrada: N = 28 
Salida: No 
 

Enfoque: La idea es ejecutar dos bucles de i y j, para encontrar la suma de todos los números enteros desde i hasta que la suma sea >= N. Si en algún momento la suma se vuelve igual a N, entonces también verificaremos si la concatenación de i y j igual a N o no. Si es igual, entonces el número es un número asombroso
. A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation for the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to concatenate
// two integers into one
int concat(int a, int b)
{
 
    // Convert both the integers to string
    string s1 = to_string(a);
    string s2 = to_string(b);
 
    // Concatenate both strings
    string s = s1 + s2;
 
    // Convert the concatenated string
    // to integer
    int c = stoi(s);
 
    // return the formed integer
    return c;
}
 
// Function to check if N is a
// Astonishing number
bool isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++) {
 
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++) {
 
            sum += j;
 
            if (sum == n) {
 
                // finding concatenation
                // of i and j
                int concatenation
                    = concat(i, j);
 
                // condition for
                // Astonishing number
                if (concatenation == n) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
// Driver Code
int main()
{
    // Given Number
    int n = 429;
 
    // Function Call
    if (isAstonishing(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java implementation for the
// above approach
import java.io.*;
class GFG{
 
// Function to concatenate
// two integers into one
static int concat(int a, int b)
{
 
    // Convert both the integers to String
    String s1 = Integer.toString(a);
    String s2 = Integer.toString(b);
 
    // Concatenate both Strings
    String s = s1 + s2;
 
    // Convert the concatenated String
    // to integer
    int c = Integer.parseInt(s);
 
    // return the formed integer
    return c;
}
 
// Function to check if N is a
// Astonishing number
static boolean isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++)
    {
 
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += j;
 
            if (sum == n)
            {
 
                // finding concatenation
                // of i and j
                int concatenation = concat(i, j);
 
                // condition for
                // Astonishing number
                if (concatenation == n)
                {
                    return true;
                }
            }
        }
    }
    return false;
}
 
// Driver Code
public static void main (String[] args)
{
    // Given Number
    int n = 429;
 
    // Function Call
    if (isAstonishing(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by shubhamsingh10

Python3

# Python3 implementation for the
# above approach
 
# Function to concatenate
# two integers into one
def concat(a, b):
     
    # Convert both the integers to string
    s1 = str(a)
    s2 = str(b)
 
    # Concatenate both strings
    s = s1 + s2
 
    # Convert the concatenated string
    # to integer
    c = int(s)
 
    # return the formed integer
    return c
 
# Function to check if N is a
# Astonishing number
def isAstonishing(n):
     
    # Loop to find sum of all integers
    # from i till the sum becomes >= n
    for i in range(n):
         
        # variable to store
        # sum of all integers
        # from i to j and
        # check if sum and
        # concatenation equals n or not
        sum = 0
        for j in range(i, n):
             
            sum += j
             
            if (sum == n):
                 
                # finding concatenation
                # of i and j
                concatenation = concat(i, j)
 
                # condition for
                # Astonishing number
                if (concatenation == n):
                    return True
    return False
     
# Driver Code
 
# Given Number
n = 429
 
# Function Call
if (isAstonishing(n)):
    print('Yes')
else:
    print('No')
 
# This code is contributed by Yatin

C#

// C# implementation for the
// above approach
using System;
class GFG{
 
// Function to concatenate
// two integers into one
static int concat(int a, int b)
{
 
    // Convert both the integers to String
    String s1 = a.ToString();
    String s2 = b.ToString();
 
    // Concatenate both Strings
    String s = s1 + s2;
 
    // Convert the concatenated String
    // to integer
    int c = Int32.Parse(s);
 
    // return the formed integer
    return c;
}
 
// Function to check if N is a
// Astonishing number
static bool isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++)
    {
 
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += j;
 
            if (sum == n)
            {
 
                // finding concatenation
                // of i and j
                int concatenation = concat(i, j);
 
                // condition for
                // Astonishing number
                if (concatenation == n)
                {
                    return true;
                }
            }
        }
    }
    return false;
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given Number
    int n = 429;
 
    // Function Call
    if (isAstonishing(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
 
// Javascript implementation for the
// above approach
 
// Function to concatenate
// two integers into one
function concat(a, b)
{
 
    // Convert both the integers to string
    var s1 = a.toString();
    var s2 = b.toString();
 
    // Concatenate both strings
    var s = s1 + s2;
 
    // Convert the concatenated string
    // to integer
    var c = s;
 
    // return the formed integer
    return c;
}
 
// Function to check if N is a
// Astonishing number
function isAstonishing(n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (var i = 1; i < n; i++) {
 
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        var sum = 0;
        for (var j = i; j < n; j++) {
 
            sum += j;
 
            if (sum == n) {
 
                // finding concatenation
                // of i and j
                var concatenation
                    = concat(i, j);
 
                // condition for
                // Astonishing number
                if (concatenation == n) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
// Driver Code
// Given Number
var n = 429;
// Function Call
if (isAstonishing(n))
    document.write( "Yes");
else
    document.write( "No");
 
</script>
Producción: 

Yes

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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