954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding magic numbers

Hello all,

I have a trouble finding Magic Numbers. I'd greatly appreciate any help in writing a function in C/C++ that will find all magic numbers (with 1000 iterations or less).
"Magic numbers" are 6 digit numbers that have the property that they are equal to the square of the sum of two 3-digit numbers when it's high-order and low-order digits are separated.

E.g:
123789 is not a magic number because:
123+789 = 912 and 912^2 = 831744, which is not the original number 123789.

998001 is a magic number because:
998+1 = 999 and 999^2 = 998001, which is the original number.

Thank you very much in advance.

VM

valleymorning
Light Poster
30 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Here's a rough protocol that will probably work:

Accept input as a sequence of 6 digits in a string. Convert input string into a numerical value and save it. Separate input string into two separate strings based on given criteria. Convert each of the two strings into numerical values. Perform each calculation as needed in appropriate sequence. Compare calculated value with stored numerical value of original string.

Now take each sentence and expand on it one sentence at a time. Always checking your work as you go. If you get stuck post appropriate code, error messages, etc. If you don't know how to do one step or another try to look it up or ask appropriate question showing work so we know you've tried.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Hi Lerner,

I appreciate your response

Here's a rough protocol that will probably work:

Accept input as a sequence of 6 digits in a string. Convert input string into a numerical value and save it. Separate input string into two separate strings based on given criteria. Convert each of the two strings into numerical values.

Actually, I have to calculate the sum of first 3 digits, then the sum of last 3 digits, then square the sum of both results. That mean I have to convert each of digit to numerical value then calculate. That's kinda cumbersome to do. Could you have an elegant way to solve and give me some examples in code? Perform each calculation as needed in appropriate sequence. Compare calculated value with stored numerical value of original string.

Now take each sentence and expand on it one sentence at a time. Always checking your work as you go. If you get stuck post appropriate code, error messages, etc. If you don't know how to do one step or another try to look it up or ask appropriate question showing work so we know you've tried.

valleymorning
Light Poster
30 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

You already have this thread in the Java section.

http://www.daniweb.com/forums/thread216043.html

Are you coding in C++ or Java?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thread closed because a duplicate exists here: http://www.daniweb.com/forums/thread216043.html

Unless you can prove that there's something language-specific for this problem, there's no need to have two threads going in two different forums.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You