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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.