-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32923f0
commit 084fa04
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
Problem Statement | ||
���� | ||
***Note: Please keep programs under 7000 characters in length. Thank you | ||
|
||
|
||
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); | ||
|
||
TopCoder 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 | ||
���� | ||
Class: | ||
SquareDigits | ||
Method: | ||
smallestResult | ||
Parameters: | ||
int | ||
Returns: | ||
int | ||
Method signature: | ||
int smallestResult(int param0) | ||
(be sure your method is public) | ||
���� | ||
|
||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
public class Main { | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
SquareDigits sd = new SquareDigits(); | ||
System.out.println(sd.smallestResult(0)); | ||
System.out.println(sd.smallestResult(2)); | ||
System.out.println(sd.smallestResult(1)); | ||
System.out.println(sd.smallestResult(19)); | ||
System.out.println(sd.smallestResult(85)); | ||
System.out.println(sd.smallestResult(112)); | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class SquareDigits { | ||
private List<Integer> _tList; | ||
|
||
public SquareDigits() | ||
{ | ||
_tList = new ArrayList<Integer>(); | ||
} | ||
|
||
public int S(int x) | ||
{ | ||
int result = 0; | ||
String s = Integer.toString(x); | ||
for (char c: s.toCharArray()) | ||
{ | ||
result += Math.pow(c - '0', 2); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public void T(int x) | ||
{ | ||
_tList.add(S(x)); | ||
|
||
int temp; | ||
while (true) | ||
{ | ||
temp = S(_tList.get(_tList.size() - 1)); | ||
if (_tList.contains(temp)) | ||
{ | ||
return; | ||
} | ||
else | ||
{ | ||
_tList.add(temp); | ||
} | ||
} | ||
} | ||
|
||
public int smallestResult(int param0) | ||
{ | ||
int smallestResult = 0; | ||
|
||
while (true) | ||
{ | ||
_tList.clear(); | ||
T(smallestResult); | ||
|
||
if (_tList.contains(param0)) | ||
{ | ||
break; | ||
} | ||
++smallestResult; | ||
} | ||
|
||
return smallestResult; | ||
} | ||
} |