Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)

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

CPP

Java

Python 3

C#


Output :

Square of 3333 is : 11108889
Square of 6666 is : 44435556
Square of 9999 is : 99980001


My Personal Notes arrow_drop_up

Recommended Posts:


    Article Tags :
    Practice Tags :



    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.