Thursday, January 9, 2025
Google search engine
HomeData Modelling & AISquares of numbers with repeated single digits | Set 1 (3, 6...

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 
Below is the implementation of above approach:
 

C++




// C++ program to find square of
// these large numbers
#include <iostream>
using namespace std;
 
// Function to find the square of
// 333...333, 666...666 and 999...999
string find_Square_369(string num)
{
    char a, b, c, d;
 
    // if the number is 333...333
    if (num[0] == '3')
        a = '1', b = '0', c = '8', d = '9';
 
    // if the number is 666...666
    else if (num[0] == '6')
        a = '4', b = '3', c = '5', d = '6';
 
    // if the number is 999...999
    else
        a = '9', b = '8', c = '0', d = '1';
 
    // variable for hold result
    string result = "";
 
    // find the no of digit
    int size = num.size();
 
    // add size-1 time a in result
    for (int i = 1; i < num.size(); i++)
        result += a;
 
    // add one time b in result
    result += b;
 
    // add size-1 time c in result
    for (int i = 1; i < num.size(); i++)
        result += c;
 
    // add one time d in result
    result += d;
 
    // return result
    return result;
}
 
// Drivers code
int main()
{
    
    string num_3, num_6, num_9;
    num_3 = "3333";
    num_6 = "6666";
    num_9 = "9999";
 
    string result = "";
 
    // find square of 33..33
    result = find_Square_369(num_3);
    cout << "Square of " << num_3 << " is : " << result << endl;
 
    // find square of 66..66
    result = find_Square_369(num_6);
    cout << "Square of " << num_6 << " is : " << result << endl;
 
    // find square of 66..66
    result = find_Square_369(num_9);
    cout << "Square of " << num_9 << " is : " << result << endl;
 
    return 0;
}


Java




// Java program to find square of
// these large numbers
import java.io.*;
public class GFG {
     
    // Function to find the square of
    // 333...333, 666...666 and 999...999
    static String find_Square_369(String num)
    {
        char a, b, c, d;
     
        // if the number is 333...333
        if (num.charAt(0) == '3')
            {a = '1'; b = '0'; c = '8'; d = '9';}
     
        // if the number is 666...666
        else if (num.charAt(0) == '6')
            {a = '4'; b = '3'; c = '5'; d = '6';}
     
        // if the number is 999...999
        else
            {a = '9'; b = '8'; c = '0'; d = '1';}
     
        // variable for hold result
        String result = "";
     
        // find the no of digit
        int size = num.length();
     
        // add size-1 time a in result
        for (int i = 1; i < size; i++)
            result += a;
     
        // add one time b in result
        result += b;
     
        // add size-1 time c in result
        for (int i = 1; i < size; i++)
            result += c;
     
        // add one time d in result
        result += d;
     
        // return result
        return result;
    }
     
    // Drivers code
    public static void main(String[] args)
    {
 
        String num_3, num_6, num_9;
        num_3 = "3333";
        num_6 = "6666";
        num_9 = "9999";
     
        String result = "";
     
        // find square of 33..33
        result = find_Square_369(num_3);
        System.out.println("Square of " + num_3
                            + " is : " + result);
     
        // find square of 66..66
        result = find_Square_369(num_6);
        System.out.println("Square of " + num_6
                            + " is : " + result);
     
        // find square of 66..66
        result = find_Square_369(num_9);
        System.out.println("Square of " + num_9
                            + " is : " + result);
     
    }
}
 
// This code is contributed by Smitha.


Python 3




# Python 3 program to find square of
# these large numbers
 
# Function to find the square of
# 333...333, 666...666 and 999...999
def find_Square_369(num):
 
    # if the number is 333...333
    if (num[0] == '3'):
        a = '1'
        b = '0'
        c = '8'
        d = '9'
 
    # if the number is 666...666
    elif (num[0] == '6'):
        a = '4'
        b = '3'
        c = '5'
        d = '6'
 
    # if the number is 999...999
    else:
        a = '9'
        b = '8'
        c = '0'
        d = '1'
 
    # variable for hold result
    result = ""
 
    # find the no of digit
    size = len(num)
 
    # add size-1 time a in result
    for i in range(1, size):
        result += a
 
    # add one time b in result
    result += b
 
    # add size-1 time c in result
    for i in range(1, size):
        result += c
 
    # add one time d in result
    result += d
 
    # return result
    return result
 
 
# Drivers code
# Your Python 3 Code
 
num_3 = "3333"
num_6 = "6666"
num_9 = "9999"
 
result = ""
 
# find square of 33..33
result = find_Square_369(num_3)
print("Square of " + num_3 + " is : "
                            + result);
 
# find square of 66..66
result = find_Square_369(num_6)
print("Square of " + num_6 + " is : "
                            + result);
 
# find square of 66..66
result = find_Square_369(num_9)
print("Square of " + num_9 + " is : "
                           + result);
 
# This code is contributed by Smitha


C#




// C# program to find square of
// these large numbers
using System;
 
class GFG {
     
    // Function to find the square of
    // 333...333, 666...666 and 999...999
    static string find_Square_369(string num)
    {
        char a, b, c, d;
     
        // if the number is 333...333
        if (num[0] == '3')
            {a = '1'; b = '0'; c = '8'; d = '9';}
     
        // if the number is 666...666
        else if (num[0] == '6')
            {a = '4'; b = '3'; c = '5'; d = '6';}
     
        // if the number is 999...999
        else
            {a = '9'; b = '8'; c = '0'; d = '1';}
     
        // variable for hold result
        string result = "";
     
        // find the no of digit
        int size = num.Length;
     
        // add size-1 time a in result
        for (int i = 1; i < size; i++)
            result += a;
     
        // add one time b in result
        result += b;
     
        // add size-1 time c in result
        for (int i = 1; i < size; i++)
            result += c;
     
        // add one time d in result
        result += d;
     
        // return result
        return result;
    }
     
    // Drivers code
    public static void Main()
    {
        string num_3, num_6, num_9;
        num_3 = "3333";
        num_6 = "6666";
        num_9 = "9999";
     
        string result = "";
     
        // find square of 33..33
        result = find_Square_369(num_3);
        Console.Write("Square of " + num_3
                + " is : " + result + "\n");
     
        // find square of 66..66
        result = find_Square_369(num_6);
        Console.Write("Square of " + num_6
                + " is : " + result + "\n");
     
        // find square of 66..66
        result = find_Square_369(num_9);
        Console.Write("Square of " + num_9
                + " is : " + result + "\n");
    }
}
 
// This code is contributed by Smitha


PHP




<?php
// PHP program to find square of
// these large numbers
  
// Function to find the square of
// 333...333, 666...666 and 999...999
function find_Square_369($num)
{
  
    // if the number is 333...333
    if ($num[0] == '3')
    {
        $a = '1';
        $b = '0';
        $c = '8';
        $d = '9';
    }
  
    // if the number is 666...666
    else if ($num[0] == '6')
    {
        $a = '4';
        $b = '3';
        $c = '5';
        $d = '6';
    }
  
    // if the number is 999...999
    else
    {
        $a = '9';
        $b = '8';
        $c = '0';
        $d = '1';
    }
  
    // variable for hold result
    $result = "";
  
    // find the no of digit
    $size = strlen($num);
  
    // add size-1 time a in result
    for ($i = 1; $i < $size; $i++)
        $result = $result.$a;
  
    // add one time b in result
    $result = $result.$b;
  
    // add size-1 time c in result
    for ($i = 1; $i < $size; $i++)
        $result = $result.$c;
  
    // add one time d in result
    $result = $result.$d;
  
    // return result
    return $result;
}
  
// Drivers code
 
$num_3 = "3333";
$num_6 = "6666";
$num_9 = "9999";
 
$result = "";
 
// find square of 33..33
$result = find_Square_369($num_3);
echo "Square of " . $num_3 . " is : " . $result ."\n" ;
 
// find square of 66..66
$result = find_Square_369($num_6);
echo "Square of " . $num_6 . " is : " .$result ."\n";
 
// find square of 66..66
$result = find_Square_369($num_9);
echo "Square of " . $num_9 . " is : ".$result ."\n";
 
return 0;
?>


Javascript




<script>
// Javascript program to find square of
// these large numbers
     
    // Function to find the square of
    // 333...333, 666...666 and 999...999
    function find_Square_369(num)
    {
        let a, b, c, d;
       
        // if the number is 333...333
        if (num[0] == '3')
            {a = '1'; b = '0'; c = '8'; d = '9';}
       
        // if the number is 666...666
        else if (num[0] == '6')
            {a = '4'; b = '3'; c = '5'; d = '6';}
       
        // if the number is 999...999
        else
            {a = '9'; b = '8'; c = '0'; d = '1';}
       
        // variable for hold result
        let result = "";
       
        // find the no of digit
        let size = num.length;
       
        // add size-1 time a in result
        for (let i = 1; i < size; i++)
            result += a;
       
        // add one time b in result
        result += b;
       
        // add size-1 time c in result
        for (let i = 1; i < size; i++)
            result += c;
       
        // add one time d in result
        result += d;
       
        // return result
        return result;
    }
     
    // Drivers code
    let num_3, num_6, num_9;
     
    num_3 = "3333";
    num_6 = "6666";
    num_9 = "9999";
       
    let result = "";
       
    // find square of 33..33
    result = find_Square_369(num_3);
    document.write("Square of " + num_3
                            + " is : " + result+"<br>");
       
    // find square of 66..66
    result = find_Square_369(num_6);
    document.write("Square of " + num_9
                            + " is : " + result+"<br>");
       
    // find square of 66..66
    result = find_Square_369(num_9);
    document.write("Square of " + num_9
                            + " is : " + result+"<br>");
     
     
    // This code is contributed by avanitrachhadiya2155
</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)

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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments