Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
Last Updated :
22 Feb, 2023
Given a number made of single digits, find its square. It may be assumed that the single digits are 3, 6 and 9. Numbers can be very large i.e. can exceed long long int.
Examples:
Input : 33 66 99
Output :
Square of 33 is : 1089
Square of 66 is : 4356
Square of 99 is : 9801
Input : 333 666 999
Output :
Square of 333 is : 110889
Square of 666 is : 443556
Square of 999 is : 998001
Method 1 (Writing as multiples of 1111…1)
An interesting fact is, every such number can be represented as a multiple of 1111…1. For example, 33333 = 3 * 11111. Squares of 11, 111, 1111, 11111 … are 121, 12321, 1234321, 123454321, … respectively. So a simple solution is find square of 111…11 then multiply the result with 3 or 6 or 9 (We can use multiplication with large number).
Method 2 (Using digit patterns)
For 333….333 Count the no. of digits and print in the below manner:
Suppose no. of digit is n then write the n-1 times 1 and then write one time 0 and then write n-1 time 8 and in last write 9.
Example :
{ 3333 } = 11108889
For 666….666 Count the no. of digits and print in the below manner:
Suppose no of digit is n then write the n-1 times 4 and then write one time 3 and then write n-1 time 5 and in last write 6.
Example :
{ 6666 } = 44435556
For 999….999 Count the no. of digits and print in the below manner:
Suppose no of digit is n then write the n-1 times 9 and then write one time 8 and then write n-1 time 0 and in last write 1.
Example :
{ 9999 } = 99980001
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
string find_Square_369(string num)
{
char a, b, c, d;
if (num[0] == '3' )
a = '1' , b = '0' , c = '8' , d = '9' ;
else if (num[0] == '6' )
a = '4' , b = '3' , c = '5' , d = '6' ;
else
a = '9' , b = '8' , c = '0' , d = '1' ;
string result = "" ;
int size = num.size();
for ( int i = 1; i < num.size(); i++)
result += a;
result += b;
for ( int i = 1; i < num.size(); i++)
result += c;
result += d;
return result;
}
int main()
{
string num_3, num_6, num_9;
num_3 = "3333" ;
num_6 = "6666" ;
num_9 = "9999" ;
string result = "" ;
result = find_Square_369(num_3);
cout << "Square of " << num_3 << " is : " << result << endl;
result = find_Square_369(num_6);
cout << "Square of " << num_6 << " is : " << result << endl;
result = find_Square_369(num_9);
cout << "Square of " << num_9 << " is : " << result << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static String find_Square_369(String num)
{
char a, b, c, d;
if (num.charAt( 0 ) == '3' )
{a = '1' ; b = '0' ; c = '8' ; d = '9' ;}
else if (num.charAt( 0 ) == '6' )
{a = '4' ; b = '3' ; c = '5' ; d = '6' ;}
else
{a = '9' ; b = '8' ; c = '0' ; d = '1' ;}
String result = "" ;
int size = num.length();
for ( int i = 1 ; i < size; i++)
result += a;
result += b;
for ( int i = 1 ; i < size; i++)
result += c;
result += d;
return result;
}
public static void main(String[] args)
{
String num_3, num_6, num_9;
num_3 = "3333" ;
num_6 = "6666" ;
num_9 = "9999" ;
String result = "" ;
result = find_Square_369(num_3);
System.out.println( "Square of " + num_3
+ " is : " + result);
result = find_Square_369(num_6);
System.out.println( "Square of " + num_6
+ " is : " + result);
result = find_Square_369(num_9);
System.out.println( "Square of " + num_9
+ " is : " + result);
}
}
|
Python 3
def find_Square_369(num):
if (num[ 0 ] = = '3' ):
a = '1'
b = '0'
c = '8'
d = '9'
elif (num[ 0 ] = = '6' ):
a = '4'
b = '3'
c = '5'
d = '6'
else :
a = '9'
b = '8'
c = '0'
d = '1'
result = ""
size = len (num)
for i in range ( 1 , size):
result + = a
result + = b
for i in range ( 1 , size):
result + = c
result + = d
return result
num_3 = "3333"
num_6 = "6666"
num_9 = "9999"
result = ""
result = find_Square_369(num_3)
print ( "Square of " + num_3 + " is : "
+ result);
result = find_Square_369(num_6)
print ( "Square of " + num_6 + " is : "
+ result);
result = find_Square_369(num_9)
print ( "Square of " + num_9 + " is : "
+ result);
|
C#
using System;
class GFG {
static string find_Square_369( string num)
{
char a, b, c, d;
if (num[0] == '3' )
{a = '1' ; b = '0' ; c = '8' ; d = '9' ;}
else if (num[0] == '6' )
{a = '4' ; b = '3' ; c = '5' ; d = '6' ;}
else
{a = '9' ; b = '8' ; c = '0' ; d = '1' ;}
string result = "" ;
int size = num.Length;
for ( int i = 1; i < size; i++)
result += a;
result += b;
for ( int i = 1; i < size; i++)
result += c;
result += d;
return result;
}
public static void Main()
{
string num_3, num_6, num_9;
num_3 = "3333" ;
num_6 = "6666" ;
num_9 = "9999" ;
string result = "" ;
result = find_Square_369(num_3);
Console.Write( "Square of " + num_3
+ " is : " + result + "\n" );
result = find_Square_369(num_6);
Console.Write( "Square of " + num_6
+ " is : " + result + "\n" );
result = find_Square_369(num_9);
Console.Write( "Square of " + num_9
+ " is : " + result + "\n" );
}
}
|
PHP
<?php
function find_Square_369( $num )
{
if ( $num [0] == '3' )
{
$a = '1' ;
$b = '0' ;
$c = '8' ;
$d = '9' ;
}
else if ( $num [0] == '6' )
{
$a = '4' ;
$b = '3' ;
$c = '5' ;
$d = '6' ;
}
else
{
$a = '9' ;
$b = '8' ;
$c = '0' ;
$d = '1' ;
}
$result = "" ;
$size = strlen ( $num );
for ( $i = 1; $i < $size ; $i ++)
$result = $result . $a ;
$result = $result . $b ;
for ( $i = 1; $i < $size ; $i ++)
$result = $result . $c ;
$result = $result . $d ;
return $result ;
}
$num_3 = "3333" ;
$num_6 = "6666" ;
$num_9 = "9999" ;
$result = "" ;
$result = find_Square_369( $num_3 );
echo "Square of " . $num_3 . " is : " . $result . "\n" ;
$result = find_Square_369( $num_6 );
echo "Square of " . $num_6 . " is : " . $result . "\n" ;
$result = find_Square_369( $num_9 );
echo "Square of " . $num_9 . " is : " . $result . "\n" ;
return 0;
?>
|
Javascript
<script>
function find_Square_369(num)
{
let a, b, c, d;
if (num[0] == '3' )
{a = '1' ; b = '0' ; c = '8' ; d = '9' ;}
else if (num[0] == '6' )
{a = '4' ; b = '3' ; c = '5' ; d = '6' ;}
else
{a = '9' ; b = '8' ; c = '0' ; d = '1' ;}
let result = "" ;
let size = num.length;
for (let i = 1; i < size; i++)
result += a;
result += b;
for (let i = 1; i < size; i++)
result += c;
result += d;
return result;
}
let num_3, num_6, num_9;
num_3 = "3333" ;
num_6 = "6666" ;
num_9 = "9999" ;
let result = "" ;
result = find_Square_369(num_3);
document.write( "Square of " + num_3
+ " is : " + result+ "<br>" );
result = find_Square_369(num_6);
document.write( "Square of " + num_9
+ " is : " + result+ "<br>" );
result = find_Square_369(num_9);
document.write( "Square of " + num_9
+ " is : " + result+ "<br>" );
</script>
|
Output :
Square of 3333 is : 11108889
Square of 6666 is : 44435556
Square of 9999 is : 99980001
Time complexity : O(n)
Space complexity : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...