|
Number pattern challenge
|
|
02-14-2010, 08:59 PM
(This post was last modified: 02-14-2010 09:01 PM by drdebcol.)
Post: #1
|
|||
|
|||
|
Number pattern challenge
This is Number pattern challenge from codecaine's site freelancecode.net or better said community : freelancecode.net/community.
Very interesting task i can say codecaine that's why i posted it here. The task is : Given the following number representing a pattern: 2053 = 2^0 + 5^3 = 1 + 125 = 126 Note: ^ means exponent Look above at the number "20"; in this sequence it represents 2 to the 0 power which is 1. The other number "53" represents 5 to the 3rd power which is 125. Then final answer is calculated by the sum of each term. Given this number below: 342345820139586830203845861938475676 What is the answer using the above pattern? Solution Well i solved it in Pascal and C++, first i represented this number above as a string and i made a sum of those powers of digits. The solution i get is : 2517052 In Pascal i used power function which i explained here : http://www.pro9ramming.com/exponentation...t-992.html So here is the code in Pascal : Code: program number_pattern;So here is the code in C++ : Code: # include <iostream>"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
02-15-2010, 05:49 AM
(This post was last modified: 02-15-2010 05:55 AM by codecaine.)
Post: #2
|
|||
|
|||
|
RE: Number pattern challenge
I revised my code in python here is how I did it. On my website I did a longer version. I just made it look neater now that I looked at my code
![]() Code: str_num = '342345820139586830203845861938475676'; #string to hold long numberPython always win when it comes to string manipulations but pretty much have the same concepts other other languages but condensed. Once again great job on the coding drdebcol. I enjoying seeing how other people solve the problems differently. |
|||
|
02-15-2010, 06:04 AM
Post: #3
|
|||
|
|||
|
RE: Number pattern challenge
Yeah Python version is much shorter. I can see you converted string to char list and than chars into int digits and went through all of them and did exponentation using "**" operator which is great innovation of Python !
Very good ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
02-15-2010, 11:11 AM
Post: #4
|
|||
|
|||
|
RE: Number pattern challenge
This is my solution to the problem written in JAVA.
Code: package NumberPattern; |
|||
|
02-15-2010, 11:35 AM
(This post was last modified: 02-15-2010 11:38 AM by codecaine.)
Post: #5
|
|||
|
|||
|
RE: Number pattern challenge
Nice java work man. I really like this function char[] str_num_array = str_num.toCharArray();
|
|||
|
02-15-2010, 06:40 PM
(This post was last modified: 02-15-2010 06:40 PM by drdebcol.)
Post: #6
|
|||
|
|||
|
RE: Number pattern challenge
Yeah very good Java code, i like the way you did that variable declaring inside of loop, like local variables. Great job !
So now we have solution of this in Pascal, C++, Python and Java ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
02-15-2010, 08:28 PM
Post: #7
|
|||
|
|||
|
RE: Number pattern challenge
C# example
Code: using System; |
|||
|
02-15-2010, 09:11 PM
Post: #8
|
|||
|
|||
|
RE: Number pattern challenge
Ha ha very good, another language from C-family !
Now we have solutions in Pascal, C++, Python, Java and C# ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
02-17-2010, 04:33 AM
Post: #9
|
|||
|
|||
|
RE: Number pattern challenge
An optimized solution would be to count all of the occurrences of each digit in the odd positions of the string.
For example, if the string is ‘212324’ it would be much faster to read it as 2*(2^2*(1+2)) (this is of course for longer strings), so that the number of calculations is reduced. Of course, the implementation of such an algorithm would take more time, but the algorithm itself would be significantly more efficient. In the above example, using your algorithm the solution would be 2^1+2^3+2^4 which is 2+2*2*2+2*2*2*2 (n=7 operations), but using the optimized algorithm would yield 2*(2*2*(1+2)) which is n=4 operations. Therefore the speed of the algorithm almost doubles. For longer strings the optimization is obviously much higher. This type of optimization is not appropriate for short strings because the number of operations required to retrieve the optimized form exceeds the number of operations required to perform a non-optimized calculation. But for strings longer than 81 characters the modified algorithm will always be more efficient than the simple one. Sorry for not providing code, but I just wanted to make a short illustration. The coding part I leave to those who want to practice. Implementation is not difficult.
MoneyBookers.com - Internet Banking When the power of love overcomes the love of power, the world will know peace. |
|||
|
02-17-2010, 05:14 AM
Post: #10
|
|||
|
|||
|
RE: Number pattern challenge
Very interesting XIO-X2, this is certainly faster. I must say that this optimization can make this much faster and in bigger numbers it can even double speed because it can find more occurrences of some digits.
THX for this optimization ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
« Next Oldest | Next Newest »
|









