Wednesday, July 3, 2024

Astonishing Numbers

Astonishing Number is a number N whose representation can be decomposed into two parts, a and b, such that N is equal to the sum of the integers from a to b and a + b = N where ‘+’ denotes concatenation.
Few Astonishing Numbers are: 
 

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

Check if N is an Astonishing number

Given a number N, the task is to check if N is an Astonishing Number or not. If N is an Astonishing Number then print “Yes” else print “No”.
Examples: 
 

Input: N = 429 
Output: Yes 
Explanation: 
429 = 4 + 5 + 6 …….. + 29, where a = 4, b = 29 
and a + b = 429 where + denotes concatenation
Input: N = 28 
Output: No 
 

Approach: The idea is to run two loops of i and j, to find the sum of all integers from i till the sum becomes >= N. If at any point of time the sum becomes equals to N then we will also check if the concatenation of i and j equal to N or not. If equals then the number is an Astonishing number
Below is the implementation of the above approach:
 

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>


Output: 

Yes

 

Time Complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments