Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 7:13 AM   #1
codecreator
Newbie
 
codecreator's Avatar
 
Join Date: Nov 2005
Posts: 1
Rep Power: 0 codecreator is on a distinguished road
Mathematical Problem

Please help me to solve this problem:

Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int


Define the function S(x) as the sum of the squares of the digits of x.
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.

Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x. That is: S(x), S(S(x)), S(S(S(x))), etc...
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.

Implement a class SquareDigits, which contains a method smallestResult. The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.

The method signature is (be sure your method is public):
int smallestResult(int n);

You will ensure n is non-negative and is between 0 and 199 inclusive.

Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.

If n=2: T(0) through T(10) do not contain the value 2. If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.

If n=10: T(0) through T(6) do not contain 10. If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat...
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.

n=1 -> x=1
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition
****
ClassquareDigits

Method:smallestResult

Parameters:int

Returns:int

Method signature:int smallestResult(int param0)
****************************************************
With Regards
codecreator is offline   Reply With Quote
Old Nov 30th, 2005, 7:48 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Please read "how to post a question", what do you want help with?
Be sure to use code tags and show us what code you already have.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 30th, 2005, 1:34 PM   #3
Munsta
Newbie
 
Join Date: Nov 2005
Posts: 9
Rep Power: 0 Munsta is on a distinguished road
Did you start coding, did you create at least S(x)? Don't expect people to do your homework for you but someone can help you fix problems that you run into while working on the assigment.
Munsta is offline   Reply With Quote
Old Nov 30th, 2005, 6:57 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Is this a problem off of TopCoder or something? I _know_ I have seen this before.
andro is offline   Reply With Quote
Old Dec 2nd, 2005, 2:59 PM   #5
boraciner
Programmer
 
boraciner's Avatar
 
Join Date: Nov 2005
Location: Turkey
Posts: 93
Rep Power: 3 boraciner is on a distinguished road
I suggest that you should use a math function which is % ....
boraciner is offline   Reply With Quote
Old Dec 3rd, 2005, 3:38 AM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by andro
Is this a problem off of TopCoder or something? I _know_ I have seen this before.
Looks like homework to me. It also looks like the OP has made no effort, as there were no details in the post regarding what part of the problem was, uhm, problematic.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 3rd, 2005, 8:13 AM   #7
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
I havent used c++ in a few years, but I was looking at this yesterday at work and wrote it in java in just a few minutes. So I'm sure it can be done in c++ fairly easy as well. Show us what you have, if anything. If I get bored today, I'll write it in c++...but dont count on it since its a weekend, more likely it will be monday at work. haha

JD
JDStud6 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC