Astonishing Numbers
Last Updated :
16 Jul, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int concat( int a, int b)
{
string s1 = to_string(a);
string s2 = to_string(b);
string s = s1 + s2;
int c = stoi(s);
return c;
}
bool isAstonishing( int n)
{
for ( int i = 1; i < n; i++) {
int sum = 0;
for ( int j = i; j < n; j++) {
sum += j;
if (sum == n) {
int concatenation
= concat(i, j);
if (concatenation == n) {
return true ;
}
}
}
}
return false ;
}
int main()
{
int n = 429;
if (isAstonishing(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int concat( int a, int b)
{
String s1 = Integer.toString(a);
String s2 = Integer.toString(b);
String s = s1 + s2;
int c = Integer.parseInt(s);
return c;
}
static boolean isAstonishing( int n)
{
for ( int i = 1 ; i < n; i++)
{
int sum = 0 ;
for ( int j = i; j < n; j++)
{
sum += j;
if (sum == n)
{
int concatenation = concat(i, j);
if (concatenation == n)
{
return true ;
}
}
}
}
return false ;
}
public static void main (String[] args)
{
int n = 429 ;
if (isAstonishing(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def concat(a, b):
s1 = str (a)
s2 = str (b)
s = s1 + s2
c = int (s)
return c
def isAstonishing(n):
for i in range (n):
sum = 0
for j in range (i, n):
sum + = j
if ( sum = = n):
concatenation = concat(i, j)
if (concatenation = = n):
return True
return False
n = 429
if (isAstonishing(n)):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
class GFG{
static int concat( int a, int b)
{
String s1 = a.ToString();
String s2 = b.ToString();
String s = s1 + s2;
int c = Int32.Parse(s);
return c;
}
static bool isAstonishing( int n)
{
for ( int i = 1; i < n; i++)
{
int sum = 0;
for ( int j = i; j < n; j++)
{
sum += j;
if (sum == n)
{
int concatenation = concat(i, j);
if (concatenation == n)
{
return true ;
}
}
}
}
return false ;
}
public static void Main(String[] args)
{
int n = 429;
if (isAstonishing(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function concat(a, b)
{
var s1 = a.toString();
var s2 = b.toString();
var s = s1 + s2;
var c = s;
return c;
}
function isAstonishing(n)
{
for ( var i = 1; i < n; i++) {
var sum = 0;
for ( var j = i; j < n; j++) {
sum += j;
if (sum == n) {
var concatenation
= concat(i, j);
if (concatenation == n) {
return true ;
}
}
}
}
return false ;
}
var n = 429;
if (isAstonishing(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...