Demlo number (Square of 11…1)
Given a number of the form 11..1 such that number of digits in it is smaller than 10, find square of the number.
Examples:
Input : 111111 Output : 12345654321 Input : 1111 Output : 1234321
Squares of 11…1 (where length is smaller than 10) are called Demlo numbers. A demlo number is number which consist of 1, 2, 3….n, n-1, n-2, …..1.
Demlo numbers are:
1 = 1 11 * 11 = 121 111 * 111 = 12321 1111 * 1111 = 1234321
To find demlo number, first we find append n numbers which are increasing and then append numbers which are decreasing.
C++
// CPP program to print DemloNumber #include <bits/stdc++.h> using namespace std; // To return demlo number. This function assumes // that the length of str is smaller than 10. string printDemlo(string str) { int len = str.length(); string res = "" ; // Add numbers to res upto size // of str and then add number // reverse to it for ( int i = 1; i <= len; i++) res += char (i + '0' ); for ( int i = len - 1; i >= 1; i--) res += char (i + '0' ); return res; } // Driver program to test printDemlo() int main() { string str = "111111" ; cout << printDemlo(str); return 0; } |
Java
// Java program to print DemloNumber public class Main { // To return demlo number. This function assumes // that the length of str is smaller than 10. static String printDemlo(String str) { int len = str.length(); String res = "" ; // Add numbers to res upto size // of str and then add number // reverse to it for ( int i = 1 ; i <= len; i++) res += Integer.toString(i); for ( int i = len - 1 ; i >= 1 ; i--) res += Integer.toString(i); return res; } // Driver program to test printDemlo() public static void main(String[] args) { String str = "111111" ; System.out.println(printDemlo(str)); } } |
Python3
# Python program to print Demlo Number # To return demlo number # Length of s is smaller than 10 def printDemlo(s): l = len (s) res = "" # Add numbers to res upto size # of s then add in reverse for i in range ( 1 ,l + 1 ): res = res + str (i) for i in range (l - 1 , 0 , - 1 ): res = res + str (i) return res # Driver Code s = "111111" print (printDemlo(s)) # Contributed by Harshit Agrawal |
C#
// C# program to print DemloNumber using System; public class GFG { // To return demlo number. This function assumes // that the length of str is smaller than 10. static String printDemlo(String str) { int len = str.Length; String res = "" ; // Add numbers to res upto size // of str and then add number // reverse to it for ( int i = 1; i <= len; i++) res += i.ToString(); for ( int i = len - 1; i >= 1; i--) res += i.ToString(); return res; } // Driver program to test printDemlo() public static void Main() { String str = "111111" ; Console.WriteLine(printDemlo(str)); } } // This code is contributed by mits |
PHP
<?php // PHP program to print DemloNumber // To return demlo number. This function // assumes that the length of str is // smaller than 10. function printDemlo( $str ) { $len = strlen ( $str ); $res = "" ; // Add numbers to res upto size // of str and then add number // reverse to it for ( $i = 1; $i <= $len ; $i ++) $res .= chr ( $i + 48); for ( $i = $len - 1; $i >= 1; $i --) $res .= chr ( $i + 48); return $res ; } // Driver Code $str = "111111" ; echo printDemlo( $str ); // This code is contributed by mits ?> |
Javascript
<script> // Javascript program to print DemloNumber // To return demlo number. This function assumes // that the length of str is smaller than 10. function printDemlo(str) { let len = str.length; let res = "" ; // Add numbers to res upto size // of str and then add number // reverse to it for (let i = 1; i <= len; i++) res += i.toString(); for (let i = len - 1; i >= 1; i--) res += i.toString(); return res; } // driver program let str = "111111" ; document.write(printDemlo(str)); // This code is contributed by susmitakundugoaldanga. </script> |
Output:
12345654321
Time complexity: O(log N) where N is the length of string “str” of number n
Auxiliary space: O(1) because it is using constant space
This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...