Results 1 to 2 of 2
Thread: Array
- 03-07-2010, 12:45 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
- 03-07-2010, 01:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
This can easily be solved using a little recursive method; a few simple observations:
1) let S(n) be the function we're looking for.
2) the sum of the digits in the number zero (0) equals 0, i.e. S(0) == 0
2) let a number be written as the digits d_1 d_2 ... d_n then the sum of all the digits equals S(d_1 d_2 ... d_n-1)+d_n
Observations 2) and 3) lead to the following function:
As you can see no array is needed at all.Java Code:int S(int n) { if (n == 0) return 0; // observation 2) return S(n/10)+n%10; // observation 3) }
kind regards,
JosLast edited by JosAH; 03-07-2010 at 01:15 PM.
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks