Sum of all digits for a given Positive Number [closed]
Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3. what i am doing wrong here?
public class DigitSum
{
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
return Sum+MethParam;
}
public static void main(String args)
{
DigitSum ds = new DigitSum();
System.out.println(ds.compute(435));
}
}
java algorithm
closed as too localized by Brian Roach, Konstantin Dinev, EdChum, RaYell, Seki Dec 21 '12 at 11:47
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3. what i am doing wrong here?
public class DigitSum
{
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
return Sum+MethParam;
}
public static void main(String args)
{
DigitSum ds = new DigitSum();
System.out.println(ds.compute(435));
}
}
java algorithm
closed as too localized by Brian Roach, Konstantin Dinev, EdChum, RaYell, Seki Dec 21 '12 at 11:47
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08
|
show 1 more comment
Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3. what i am doing wrong here?
public class DigitSum
{
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
return Sum+MethParam;
}
public static void main(String args)
{
DigitSum ds = new DigitSum();
System.out.println(ds.compute(435));
}
}
java algorithm
Method return should be like if entered a number, suppose 345, then output should be 3+4+5=12 --> 1+2 = 3. what i am doing wrong here?
public class DigitSum
{
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
return Sum+MethParam;
}
public static void main(String args)
{
DigitSum ds = new DigitSum();
System.out.println(ds.compute(435));
}
}
java algorithm
java algorithm
edited Jan 3 '13 at 5:49
Rahul
11.8k32751
11.8k32751
asked Dec 21 '12 at 7:08
ManishSManishS
3842517
3842517
closed as too localized by Brian Roach, Konstantin Dinev, EdChum, RaYell, Seki Dec 21 '12 at 11:47
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too localized by Brian Roach, Konstantin Dinev, EdChum, RaYell, Seki Dec 21 '12 at 11:47
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08
|
show 1 more comment
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08
|
show 1 more comment
13 Answers
13
active
oldest
votes
O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
|
show 6 more comments
What you are looking for is digital root. So here's a better solution using the formula from wiki
page I linked.
Without Recursion: -
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -
public static int compute( int n ) {
return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
add a comment |
public int FindSumDigit(int number)
{
if (number < 10) return number;
int sum = 0;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
return FindSumDigit(sum);
}
Find my code...
Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
add a comment |
Many wrong answers here. Here's what OP wants:
Method return should be like if entered a number, suppose 345, then
output should be 3+4+5=12 --> 1+2 = 3.
This will do the job:
public static int compute(int param) {
int sum = 0;
do {
sum = 0;
while (param > 0) {
sum += param % 10;
param /= 10;
}
param = sum;
} while (sum >= 10);
return sum;
}
add a comment |
I changed your method to this, then it gives the requested result:
public int compute(int methParam) {
int sum = 0;
for (int i = 0; methParam > 10; i++) {
int currentDigit = methParam % 10;
methParam = methParam / 10;
sum = sum + currentDigit;
}
if (sum + methParam > 10) {
return compute(sum + methParam);
} else {
return sum + methParam;
}
}
Please note that i moved the declaration of sum
inside the method, instead of making it a field.
Ah!! You are increasing too much load. Why that need oflogarithmic computation
? Just iterate till the number is greater than0
.
– Rohit Jain
Dec 21 '12 at 7:21
1
This answer also returns12
for input345
, OP wants output to be3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
add a comment |
IN your code your are not properly returning values to get call for your recursion method.
if ((MethParam >= 10)){
return compute(MethParam);
}else
return Sum + MethParam;
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
add a comment |
public int compute( int param )
{
int x = param % 10;
int y = param / 10;
if (y > 0)
return x + compute(y);
return x;
}
public int computeNonRec(int param) {
int result = 0;
while (param > 0) {
result += param % 10;
param /= 10;
}
return result;
}
add a comment |
Try
public int sumDigit(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
number = n / 10;
}
return sum;
}
add a comment |
just a different approach, may not be as efficient due to involved conversions:
private static int getUltimateSum(int input) {
String inputStr = String.valueOf(input);
int sum = 0;
for (int i = 0; i < inputStr.length(); i++) {
int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i)));
sum += digit;
}
if(sum > 9)
return getUltimateSum(sum);
else
return sum;
}
add a comment |
Here's a string solution:
public int compute( int MethParam )
{
int sum = 0;
string meth = MethParam.ToString();
for (char x in meth) {
sum += int.Parse(x);
}
if (sum >= 10) {
return compute(sum);
}
else {
return sum;
}
}
This code is in C# not Java so consider it pseudocode.
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
add a comment |
try
public class DigitSum {
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
else
Sum+=MethParam;
if(Sum>=10){
int temp=Sum;
Sum=0;
compute(temp);
}
return Sum;
}
public static void main(String args){
DigitSum ds= new DigitSum();
System.out.println(ds.compute(435));
}
}
1
have you tried before posting? for input345
OP wants3
, you return12
.
– jlordo
Dec 21 '12 at 7:48
add a comment |
shortest code for this is -
int compute(int n){
while(n > 9){
n = n - 9;
}
return n;
}
where n is number for which you want to compute some of its digits.
EDIT: Efficient for only small numbers, say 3 digit number.
EDIT: Tested few answers:
public class Test {
public static void main(String args){
int i = 0x0fefefef;
long st1 = System.nanoTime();
int n1 = sumDigit(i);
long t1 = System.nanoTime() - st1;
long st2 = System.nanoTime();
int n2 = FindSumDigit(i);
long t2 = System.nanoTime() - st2;
long st3 = System.nanoTime();
int n3 = compute(i);
long t3 = System.nanoTime() - st3;
long st4 = System.nanoTime();
int n4 = compute1(i);
long t4 = System.nanoTime() - st4;
System.out.println("Tested for: "+i);
System.out.println(n1+": "+t1);
System.out.println(n2+": "+t2);
System.out.println(n3+": "+t3);
System.out.println(n4+": "+t4);
}
public static int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
public static int FindSumDigit(int n)
{
if (n < 10) return n;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return FindSumDigit(sum);
}
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
public static int compute1(int n){
while(n > 9){
n = n - 9;
}
return n;
}
}
Here is result for above test:
Tested for: 267382767
3: 2432
3: 1621
3: 810
3: 5354519
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for9999999
. It will take1111110
iterations.
– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
|
show 3 more comments
use this simple java code I created some tymz ago, this was for adding positive numbers as well as negative numbers:
class SumDigit
{
public static void main(String args)
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.for(i=1;i< =10;i++)
and won't produce output3
for input345
, which is what OP is looking for.
– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
add a comment |
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
|
show 6 more comments
O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
|
show 6 more comments
O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
O(1) Algo for sum Of digits :
Taking the modulo 9 of any number will return the sum of digits of that number until a single digit number is obtained.
If the number is a multiple of 9, then the sum will will be 9
one liner :
public int sumDigit(int n){
return (n%9 == 0 && n != 0) ? 9 : n%9;
}
Alternate implementation :
public int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
edited Dec 21 '12 at 9:36
answered Dec 21 '12 at 7:29
RahulRahul
11.8k32751
11.8k32751
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
|
show 6 more comments
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
Can you please specify the error as the code is working absolutely fine on my machine.
– Rahul
Dec 21 '12 at 7:39
1
1
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
Thanks for the trick..dint know that
– ManishS
Dec 21 '12 at 7:44
3
3
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
This is an awesome solution
– GeekRide
Dec 21 '12 at 7:47
2
2
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
This is just plain wrong. sumDigit(77) returns 5 instead of 14. It never works with sums > 9
– user3207838
Jun 16 '16 at 14:49
1
1
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
@Lirik You should've looked at the OP's example first. For 345, then output should be 3+4+5=12 --> 1+2 = 3. You need to repeatedly add all digits of the result until you get a single digit
– Rahul
May 16 '17 at 4:42
|
show 6 more comments
What you are looking for is digital root. So here's a better solution using the formula from wiki
page I linked.
Without Recursion: -
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -
public static int compute( int n ) {
return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
add a comment |
What you are looking for is digital root. So here's a better solution using the formula from wiki
page I linked.
Without Recursion: -
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -
public static int compute( int n ) {
return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
add a comment |
What you are looking for is digital root. So here's a better solution using the formula from wiki
page I linked.
Without Recursion: -
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -
public static int compute( int n ) {
return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
What you are looking for is digital root. So here's a better solution using the formula from wiki
page I linked.
Without Recursion: -
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
And, just in case you want (Which I don't think you would), here's a one-liner (Using Recursion): -
public static int compute( int n ) {
return n < 10 ? n : compute(n % 10 + compute(n / 10));
}
edited Dec 21 '12 at 8:13
answered Dec 21 '12 at 7:42
Rohit JainRohit Jain
166k33314443
166k33314443
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
add a comment |
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
+1 it won't get better than that.
– jlordo
Dec 21 '12 at 7:47
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
dood one word..that is Dope :)
– ManishS
Dec 21 '12 at 8:00
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
can't get better solution than this, without recursion.
– nullptr
Dec 21 '12 at 9:02
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
Note for any fellow Googlers that digital root is not the same as the sum of digits. Digital root is iterated sum of digits operations.
– Luke Salamone
Sep 26 '16 at 5:37
add a comment |
public int FindSumDigit(int number)
{
if (number < 10) return number;
int sum = 0;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
return FindSumDigit(sum);
}
Find my code...
Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
add a comment |
public int FindSumDigit(int number)
{
if (number < 10) return number;
int sum = 0;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
return FindSumDigit(sum);
}
Find my code...
Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
add a comment |
public int FindSumDigit(int number)
{
if (number < 10) return number;
int sum = 0;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
return FindSumDigit(sum);
}
Find my code...
Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.
public int FindSumDigit(int number)
{
if (number < 10) return number;
int sum = 0;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
return FindSumDigit(sum);
}
Find my code...
Poon you were not adding the whole digits.. In middle itself u was keep on adding the right most digit.
answered Dec 21 '12 at 7:21
GauravGaurav
161137
161137
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
add a comment |
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
+1 Elegant solution. Like it better than mine, and unlike most of the answers here returns the desired result.
– jlordo
Dec 21 '12 at 7:34
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:09
Neater recursive version of this:
public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
Neater recursive version of this:
public static int sum(int n) { if(n < 10) return n; return sum(n / 10) + n%10; }
– cowls
Feb 18 '13 at 12:14
add a comment |
Many wrong answers here. Here's what OP wants:
Method return should be like if entered a number, suppose 345, then
output should be 3+4+5=12 --> 1+2 = 3.
This will do the job:
public static int compute(int param) {
int sum = 0;
do {
sum = 0;
while (param > 0) {
sum += param % 10;
param /= 10;
}
param = sum;
} while (sum >= 10);
return sum;
}
add a comment |
Many wrong answers here. Here's what OP wants:
Method return should be like if entered a number, suppose 345, then
output should be 3+4+5=12 --> 1+2 = 3.
This will do the job:
public static int compute(int param) {
int sum = 0;
do {
sum = 0;
while (param > 0) {
sum += param % 10;
param /= 10;
}
param = sum;
} while (sum >= 10);
return sum;
}
add a comment |
Many wrong answers here. Here's what OP wants:
Method return should be like if entered a number, suppose 345, then
output should be 3+4+5=12 --> 1+2 = 3.
This will do the job:
public static int compute(int param) {
int sum = 0;
do {
sum = 0;
while (param > 0) {
sum += param % 10;
param /= 10;
}
param = sum;
} while (sum >= 10);
return sum;
}
Many wrong answers here. Here's what OP wants:
Method return should be like if entered a number, suppose 345, then
output should be 3+4+5=12 --> 1+2 = 3.
This will do the job:
public static int compute(int param) {
int sum = 0;
do {
sum = 0;
while (param > 0) {
sum += param % 10;
param /= 10;
}
param = sum;
} while (sum >= 10);
return sum;
}
answered Dec 21 '12 at 7:27
jlordojlordo
31.2k64371
31.2k64371
add a comment |
add a comment |
I changed your method to this, then it gives the requested result:
public int compute(int methParam) {
int sum = 0;
for (int i = 0; methParam > 10; i++) {
int currentDigit = methParam % 10;
methParam = methParam / 10;
sum = sum + currentDigit;
}
if (sum + methParam > 10) {
return compute(sum + methParam);
} else {
return sum + methParam;
}
}
Please note that i moved the declaration of sum
inside the method, instead of making it a field.
Ah!! You are increasing too much load. Why that need oflogarithmic computation
? Just iterate till the number is greater than0
.
– Rohit Jain
Dec 21 '12 at 7:21
1
This answer also returns12
for input345
, OP wants output to be3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
add a comment |
I changed your method to this, then it gives the requested result:
public int compute(int methParam) {
int sum = 0;
for (int i = 0; methParam > 10; i++) {
int currentDigit = methParam % 10;
methParam = methParam / 10;
sum = sum + currentDigit;
}
if (sum + methParam > 10) {
return compute(sum + methParam);
} else {
return sum + methParam;
}
}
Please note that i moved the declaration of sum
inside the method, instead of making it a field.
Ah!! You are increasing too much load. Why that need oflogarithmic computation
? Just iterate till the number is greater than0
.
– Rohit Jain
Dec 21 '12 at 7:21
1
This answer also returns12
for input345
, OP wants output to be3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
add a comment |
I changed your method to this, then it gives the requested result:
public int compute(int methParam) {
int sum = 0;
for (int i = 0; methParam > 10; i++) {
int currentDigit = methParam % 10;
methParam = methParam / 10;
sum = sum + currentDigit;
}
if (sum + methParam > 10) {
return compute(sum + methParam);
} else {
return sum + methParam;
}
}
Please note that i moved the declaration of sum
inside the method, instead of making it a field.
I changed your method to this, then it gives the requested result:
public int compute(int methParam) {
int sum = 0;
for (int i = 0; methParam > 10; i++) {
int currentDigit = methParam % 10;
methParam = methParam / 10;
sum = sum + currentDigit;
}
if (sum + methParam > 10) {
return compute(sum + methParam);
} else {
return sum + methParam;
}
}
Please note that i moved the declaration of sum
inside the method, instead of making it a field.
edited Dec 21 '12 at 7:28
answered Dec 21 '12 at 7:19
FrankFrank
13.1k63246
13.1k63246
Ah!! You are increasing too much load. Why that need oflogarithmic computation
? Just iterate till the number is greater than0
.
– Rohit Jain
Dec 21 '12 at 7:21
1
This answer also returns12
for input345
, OP wants output to be3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
add a comment |
Ah!! You are increasing too much load. Why that need oflogarithmic computation
? Just iterate till the number is greater than0
.
– Rohit Jain
Dec 21 '12 at 7:21
1
This answer also returns12
for input345
, OP wants output to be3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
Ah!! You are increasing too much load. Why that need of
logarithmic computation
? Just iterate till the number is greater than 0
.– Rohit Jain
Dec 21 '12 at 7:21
Ah!! You are increasing too much load. Why that need of
logarithmic computation
? Just iterate till the number is greater than 0
.– Rohit Jain
Dec 21 '12 at 7:21
1
1
This answer also returns
12
for input 345
, OP wants output to be 3
– jlordo
Dec 21 '12 at 7:21
This answer also returns
12
for input 345
, OP wants output to be 3
– jlordo
Dec 21 '12 at 7:21
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
I edited the method to output 3 and removed the logarithmic, it was indeed overkill.
– Frank
Dec 21 '12 at 7:28
add a comment |
IN your code your are not properly returning values to get call for your recursion method.
if ((MethParam >= 10)){
return compute(MethParam);
}else
return Sum + MethParam;
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
add a comment |
IN your code your are not properly returning values to get call for your recursion method.
if ((MethParam >= 10)){
return compute(MethParam);
}else
return Sum + MethParam;
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
add a comment |
IN your code your are not properly returning values to get call for your recursion method.
if ((MethParam >= 10)){
return compute(MethParam);
}else
return Sum + MethParam;
IN your code your are not properly returning values to get call for your recursion method.
if ((MethParam >= 10)){
return compute(MethParam);
}else
return Sum + MethParam;
answered Dec 21 '12 at 7:29
SmitSmit
4,18112027
4,18112027
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
add a comment |
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
thanks, but i think it still not fulfilling the O/P like 445 should give 4 instead of 13.
– ManishS
Dec 21 '12 at 7:35
add a comment |
public int compute( int param )
{
int x = param % 10;
int y = param / 10;
if (y > 0)
return x + compute(y);
return x;
}
public int computeNonRec(int param) {
int result = 0;
while (param > 0) {
result += param % 10;
param /= 10;
}
return result;
}
add a comment |
public int compute( int param )
{
int x = param % 10;
int y = param / 10;
if (y > 0)
return x + compute(y);
return x;
}
public int computeNonRec(int param) {
int result = 0;
while (param > 0) {
result += param % 10;
param /= 10;
}
return result;
}
add a comment |
public int compute( int param )
{
int x = param % 10;
int y = param / 10;
if (y > 0)
return x + compute(y);
return x;
}
public int computeNonRec(int param) {
int result = 0;
while (param > 0) {
result += param % 10;
param /= 10;
}
return result;
}
public int compute( int param )
{
int x = param % 10;
int y = param / 10;
if (y > 0)
return x + compute(y);
return x;
}
public int computeNonRec(int param) {
int result = 0;
while (param > 0) {
result += param % 10;
param /= 10;
}
return result;
}
answered Dec 21 '12 at 7:16
Christian ThiemeChristian Thieme
1,02666
1,02666
add a comment |
add a comment |
Try
public int sumDigit(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
number = n / 10;
}
return sum;
}
add a comment |
Try
public int sumDigit(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
number = n / 10;
}
return sum;
}
add a comment |
Try
public int sumDigit(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
number = n / 10;
}
return sum;
}
Try
public int sumDigit(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
number = n / 10;
}
return sum;
}
answered Dec 21 '12 at 7:25
Evgeniy DorofeevEvgeniy Dorofeev
106k23143223
106k23143223
add a comment |
add a comment |
just a different approach, may not be as efficient due to involved conversions:
private static int getUltimateSum(int input) {
String inputStr = String.valueOf(input);
int sum = 0;
for (int i = 0; i < inputStr.length(); i++) {
int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i)));
sum += digit;
}
if(sum > 9)
return getUltimateSum(sum);
else
return sum;
}
add a comment |
just a different approach, may not be as efficient due to involved conversions:
private static int getUltimateSum(int input) {
String inputStr = String.valueOf(input);
int sum = 0;
for (int i = 0; i < inputStr.length(); i++) {
int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i)));
sum += digit;
}
if(sum > 9)
return getUltimateSum(sum);
else
return sum;
}
add a comment |
just a different approach, may not be as efficient due to involved conversions:
private static int getUltimateSum(int input) {
String inputStr = String.valueOf(input);
int sum = 0;
for (int i = 0; i < inputStr.length(); i++) {
int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i)));
sum += digit;
}
if(sum > 9)
return getUltimateSum(sum);
else
return sum;
}
just a different approach, may not be as efficient due to involved conversions:
private static int getUltimateSum(int input) {
String inputStr = String.valueOf(input);
int sum = 0;
for (int i = 0; i < inputStr.length(); i++) {
int digit = Integer.valueOf(String.valueOf(inputStr.charAt(i)));
sum += digit;
}
if(sum > 9)
return getUltimateSum(sum);
else
return sum;
}
answered Dec 21 '12 at 7:40
vishal_aimvishal_aim
5,19111222
5,19111222
add a comment |
add a comment |
Here's a string solution:
public int compute( int MethParam )
{
int sum = 0;
string meth = MethParam.ToString();
for (char x in meth) {
sum += int.Parse(x);
}
if (sum >= 10) {
return compute(sum);
}
else {
return sum;
}
}
This code is in C# not Java so consider it pseudocode.
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
add a comment |
Here's a string solution:
public int compute( int MethParam )
{
int sum = 0;
string meth = MethParam.ToString();
for (char x in meth) {
sum += int.Parse(x);
}
if (sum >= 10) {
return compute(sum);
}
else {
return sum;
}
}
This code is in C# not Java so consider it pseudocode.
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
add a comment |
Here's a string solution:
public int compute( int MethParam )
{
int sum = 0;
string meth = MethParam.ToString();
for (char x in meth) {
sum += int.Parse(x);
}
if (sum >= 10) {
return compute(sum);
}
else {
return sum;
}
}
This code is in C# not Java so consider it pseudocode.
Here's a string solution:
public int compute( int MethParam )
{
int sum = 0;
string meth = MethParam.ToString();
for (char x in meth) {
sum += int.Parse(x);
}
if (sum >= 10) {
return compute(sum);
}
else {
return sum;
}
}
This code is in C# not Java so consider it pseudocode.
edited Dec 21 '12 at 8:08
answered Dec 21 '12 at 7:34
Konstantin DinevKonstantin Dinev
24.8k105482
24.8k105482
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
add a comment |
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
1
1
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
You should mention that it's not Java. Also it doesn't return desired result.
– jlordo
Dec 21 '12 at 7:39
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
@jlordo I forgot the recursive call. Now it should return as expected.
– Konstantin Dinev
Dec 21 '12 at 7:42
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Now it'll return the correct result, but your answer should mention the programming language if it's not the one the OP uses.
– jlordo
Dec 21 '12 at 7:45
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
Oh excuse me this is C#
– Konstantin Dinev
Dec 21 '12 at 8:06
add a comment |
try
public class DigitSum {
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
else
Sum+=MethParam;
if(Sum>=10){
int temp=Sum;
Sum=0;
compute(temp);
}
return Sum;
}
public static void main(String args){
DigitSum ds= new DigitSum();
System.out.println(ds.compute(435));
}
}
1
have you tried before posting? for input345
OP wants3
, you return12
.
– jlordo
Dec 21 '12 at 7:48
add a comment |
try
public class DigitSum {
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
else
Sum+=MethParam;
if(Sum>=10){
int temp=Sum;
Sum=0;
compute(temp);
}
return Sum;
}
public static void main(String args){
DigitSum ds= new DigitSum();
System.out.println(ds.compute(435));
}
}
1
have you tried before posting? for input345
OP wants3
, you return12
.
– jlordo
Dec 21 '12 at 7:48
add a comment |
try
public class DigitSum {
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
else
Sum+=MethParam;
if(Sum>=10){
int temp=Sum;
Sum=0;
compute(temp);
}
return Sum;
}
public static void main(String args){
DigitSum ds= new DigitSum();
System.out.println(ds.compute(435));
}
}
try
public class DigitSum {
int Sum=0;
public int compute( int MethParam )
{
int rem = MethParam%10;
Sum+=rem;
MethParam = MethParam/10;
if(MethParam>10)
compute(MethParam);
else
Sum+=MethParam;
if(Sum>=10){
int temp=Sum;
Sum=0;
compute(temp);
}
return Sum;
}
public static void main(String args){
DigitSum ds= new DigitSum();
System.out.println(ds.compute(435));
}
}
edited Dec 21 '12 at 8:15
answered Dec 21 '12 at 7:42
hzzhzz
11
11
1
have you tried before posting? for input345
OP wants3
, you return12
.
– jlordo
Dec 21 '12 at 7:48
add a comment |
1
have you tried before posting? for input345
OP wants3
, you return12
.
– jlordo
Dec 21 '12 at 7:48
1
1
have you tried before posting? for input
345
OP wants 3
, you return 12
.– jlordo
Dec 21 '12 at 7:48
have you tried before posting? for input
345
OP wants 3
, you return 12
.– jlordo
Dec 21 '12 at 7:48
add a comment |
shortest code for this is -
int compute(int n){
while(n > 9){
n = n - 9;
}
return n;
}
where n is number for which you want to compute some of its digits.
EDIT: Efficient for only small numbers, say 3 digit number.
EDIT: Tested few answers:
public class Test {
public static void main(String args){
int i = 0x0fefefef;
long st1 = System.nanoTime();
int n1 = sumDigit(i);
long t1 = System.nanoTime() - st1;
long st2 = System.nanoTime();
int n2 = FindSumDigit(i);
long t2 = System.nanoTime() - st2;
long st3 = System.nanoTime();
int n3 = compute(i);
long t3 = System.nanoTime() - st3;
long st4 = System.nanoTime();
int n4 = compute1(i);
long t4 = System.nanoTime() - st4;
System.out.println("Tested for: "+i);
System.out.println(n1+": "+t1);
System.out.println(n2+": "+t2);
System.out.println(n3+": "+t3);
System.out.println(n4+": "+t4);
}
public static int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
public static int FindSumDigit(int n)
{
if (n < 10) return n;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return FindSumDigit(sum);
}
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
public static int compute1(int n){
while(n > 9){
n = n - 9;
}
return n;
}
}
Here is result for above test:
Tested for: 267382767
3: 2432
3: 1621
3: 810
3: 5354519
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for9999999
. It will take1111110
iterations.
– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
|
show 3 more comments
shortest code for this is -
int compute(int n){
while(n > 9){
n = n - 9;
}
return n;
}
where n is number for which you want to compute some of its digits.
EDIT: Efficient for only small numbers, say 3 digit number.
EDIT: Tested few answers:
public class Test {
public static void main(String args){
int i = 0x0fefefef;
long st1 = System.nanoTime();
int n1 = sumDigit(i);
long t1 = System.nanoTime() - st1;
long st2 = System.nanoTime();
int n2 = FindSumDigit(i);
long t2 = System.nanoTime() - st2;
long st3 = System.nanoTime();
int n3 = compute(i);
long t3 = System.nanoTime() - st3;
long st4 = System.nanoTime();
int n4 = compute1(i);
long t4 = System.nanoTime() - st4;
System.out.println("Tested for: "+i);
System.out.println(n1+": "+t1);
System.out.println(n2+": "+t2);
System.out.println(n3+": "+t3);
System.out.println(n4+": "+t4);
}
public static int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
public static int FindSumDigit(int n)
{
if (n < 10) return n;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return FindSumDigit(sum);
}
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
public static int compute1(int n){
while(n > 9){
n = n - 9;
}
return n;
}
}
Here is result for above test:
Tested for: 267382767
3: 2432
3: 1621
3: 810
3: 5354519
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for9999999
. It will take1111110
iterations.
– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
|
show 3 more comments
shortest code for this is -
int compute(int n){
while(n > 9){
n = n - 9;
}
return n;
}
where n is number for which you want to compute some of its digits.
EDIT: Efficient for only small numbers, say 3 digit number.
EDIT: Tested few answers:
public class Test {
public static void main(String args){
int i = 0x0fefefef;
long st1 = System.nanoTime();
int n1 = sumDigit(i);
long t1 = System.nanoTime() - st1;
long st2 = System.nanoTime();
int n2 = FindSumDigit(i);
long t2 = System.nanoTime() - st2;
long st3 = System.nanoTime();
int n3 = compute(i);
long t3 = System.nanoTime() - st3;
long st4 = System.nanoTime();
int n4 = compute1(i);
long t4 = System.nanoTime() - st4;
System.out.println("Tested for: "+i);
System.out.println(n1+": "+t1);
System.out.println(n2+": "+t2);
System.out.println(n3+": "+t3);
System.out.println(n4+": "+t4);
}
public static int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
public static int FindSumDigit(int n)
{
if (n < 10) return n;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return FindSumDigit(sum);
}
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
public static int compute1(int n){
while(n > 9){
n = n - 9;
}
return n;
}
}
Here is result for above test:
Tested for: 267382767
3: 2432
3: 1621
3: 810
3: 5354519
shortest code for this is -
int compute(int n){
while(n > 9){
n = n - 9;
}
return n;
}
where n is number for which you want to compute some of its digits.
EDIT: Efficient for only small numbers, say 3 digit number.
EDIT: Tested few answers:
public class Test {
public static void main(String args){
int i = 0x0fefefef;
long st1 = System.nanoTime();
int n1 = sumDigit(i);
long t1 = System.nanoTime() - st1;
long st2 = System.nanoTime();
int n2 = FindSumDigit(i);
long t2 = System.nanoTime() - st2;
long st3 = System.nanoTime();
int n3 = compute(i);
long t3 = System.nanoTime() - st3;
long st4 = System.nanoTime();
int n4 = compute1(i);
long t4 = System.nanoTime() - st4;
System.out.println("Tested for: "+i);
System.out.println(n1+": "+t1);
System.out.println(n2+": "+t2);
System.out.println(n3+": "+t3);
System.out.println(n4+": "+t4);
}
public static int sumDigit(int n){
int sum = n % 9;
if(sum == 0){
if(n > 0)
return 9;
}
return sum;
}
public static int FindSumDigit(int n)
{
if (n < 10) return n;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return FindSumDigit(sum);
}
public static int compute( int n ) {
return n - 9 * ((n - 1) / 9);
}
public static int compute1(int n){
while(n > 9){
n = n - 9;
}
return n;
}
}
Here is result for above test:
Tested for: 267382767
3: 2432
3: 1621
3: 810
3: 5354519
edited Dec 21 '12 at 9:31
answered Dec 21 '12 at 8:07
nullptrnullptr
2,48642658
2,48642658
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for9999999
. It will take1111110
iterations.
– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
|
show 3 more comments
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for9999999
. It will take1111110
iterations.
– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for
9999999
. It will take 1111110
iterations.– Rohit Jain
Dec 21 '12 at 8:17
This is shortest code just for writing. But it may take hell lot of iteration to complete. For e.g. try it for
9999999
. It will take 1111110
iterations.– Rohit Jain
Dec 21 '12 at 8:17
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
True Rohit, this is just another solution with some mathematics logic.
– nullptr
Dec 21 '12 at 8:22
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
Rohit, my solution is only efficient if number is less than say 2222, it is not generalized solution for number greater than 2222 in terms of CPU, but if you say number will be less than 999 for sure, my solution will be efficient, I am sure. Because if you go with solution provided by Gaurav, that solution will have overhead of method call stacks and hell lot of time on divisions and modulo operations. I have tested and found my solution efficient compared to Gaurav's solution for number less than 2222, or say only 3 digit number.
– nullptr
Dec 21 '12 at 8:50
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@user1508907.. Hmm. Yeah recursion will not give efficient performance for all input. But we cannot assume that we would get only a certain subset. So, for greater number, which of course we can get, you know what is the drawback of your method. Anyways, for more efficient solution even for greater number, see my answer.
– Rohit Jain
Dec 21 '12 at 8:54
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
@RohitJain, +25 for best solution without recursion. Can't get any more efficient than that, :)
– nullptr
Dec 21 '12 at 9:00
|
show 3 more comments
use this simple java code I created some tymz ago, this was for adding positive numbers as well as negative numbers:
class SumDigit
{
public static void main(String args)
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.for(i=1;i< =10;i++)
and won't produce output3
for input345
, which is what OP is looking for.
– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
add a comment |
use this simple java code I created some tymz ago, this was for adding positive numbers as well as negative numbers:
class SumDigit
{
public static void main(String args)
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.for(i=1;i< =10;i++)
and won't produce output3
for input345
, which is what OP is looking for.
– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
add a comment |
use this simple java code I created some tymz ago, this was for adding positive numbers as well as negative numbers:
class SumDigit
{
public static void main(String args)
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
use this simple java code I created some tymz ago, this was for adding positive numbers as well as negative numbers:
class SumDigit
{
public static void main(String args)
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}
answered Dec 21 '12 at 7:11
AzzyAzzy
398118
398118
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.for(i=1;i< =10;i++)
and won't produce output3
for input345
, which is what OP is looking for.
– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
add a comment |
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.for(i=1;i< =10;i++)
and won't produce output3
for input345
, which is what OP is looking for.
– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
4
4
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
Please. At least bother to format your code.
– Rohit Jain
Dec 21 '12 at 7:14
it won't even compile.
for(i=1;i< =10;i++)
and won't produce output 3
for input 345
, which is what OP is looking for.– jlordo
Dec 21 '12 at 7:16
it won't even compile.
for(i=1;i< =10;i++)
and won't produce output 3
for input 345
, which is what OP is looking for.– jlordo
Dec 21 '12 at 7:16
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
Why this answer is upvoted thrice??
– Rohit Jain
Dec 21 '12 at 7:19
2
2
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
@Azzy.. Well, I very rarely vote down a post. That you can see from my profile. But your code won't compile. And neither will it give correct result when run. You are iterating 10 times. Why are you sure that you need 10 iterations here? I think down votes are justified here. You improve your answer I'll remove my downvote.
– Rohit Jain
Dec 21 '12 at 7:22
add a comment |
So what's the issue? Are you getting wrong output?
– Rohit Jain
Dec 21 '12 at 7:09
try debugging with the debugger or some print statements.
– jlordo
Dec 21 '12 at 7:11
Why are you using recursion?
– Mark Byers
Dec 21 '12 at 7:11
reducing complexity may be.. imo its better than using for loop for big numbers, as some pal used below.
– ManishS
Dec 21 '12 at 7:52
please see my answer for shortest code tip..
– nullptr
Dec 21 '12 at 8:08