Calculate the sum of digits. If the number repeats itself, do not calculate it [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My question is how do I check if a digit is repeated in an integer without using arrays?
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
My current code is:
# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;
int digit;
printf("Please enter a number: ");
scanf("%d" ,&input);
while(input > 0) {
digit = input % 10;
if(d0 < 1) {
sum += digit;
d0 = 1;
}
input /= 10;
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
c
closed as too broad by Jean-François Fabre♦, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 14 more comments
My question is how do I check if a digit is repeated in an integer without using arrays?
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
My current code is:
# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;
int digit;
printf("Please enter a number: ");
scanf("%d" ,&input);
while(input > 0) {
digit = input % 10;
if(d0 < 1) {
sum += digit;
d0 = 1;
}
input /= 10;
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
c
closed as too broad by Jean-François Fabre♦, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
1
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
1
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with0
; for each digit, increment the respective counter/flag only if this counter is still0
. At the end writed1 + 2*d2 + 3*d3 ...
Ugly, but will work.
– Stephan Lechner
Nov 23 '18 at 21:18
3
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
7
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30
|
show 14 more comments
My question is how do I check if a digit is repeated in an integer without using arrays?
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
My current code is:
# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;
int digit;
printf("Please enter a number: ");
scanf("%d" ,&input);
while(input > 0) {
digit = input % 10;
if(d0 < 1) {
sum += digit;
d0 = 1;
}
input /= 10;
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
c
My question is how do I check if a digit is repeated in an integer without using arrays?
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
My current code is:
# include "stdio.h"
int main () {
int input = 0;
int sum = 0;
int input = 0;
int sum = 0;
int digit;
printf("Please enter a number: ");
scanf("%d" ,&input);
while(input > 0) {
digit = input % 10;
if(d0 < 1) {
sum += digit;
d0 = 1;
}
input /= 10;
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
c
c
edited Nov 24 '18 at 1:36
chux
85.1k874157
85.1k874157
asked Nov 23 '18 at 21:02
Ron SegalRon Segal
12
12
closed as too broad by Jean-François Fabre♦, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Jean-François Fabre♦, too honest for this site, Matthieu Brucher, stackptr, Mike M. Nov 24 '18 at 0:56
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
1
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
1
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with0
; for each digit, increment the respective counter/flag only if this counter is still0
. At the end writed1 + 2*d2 + 3*d3 ...
Ugly, but will work.
– Stephan Lechner
Nov 23 '18 at 21:18
3
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
7
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30
|
show 14 more comments
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
1
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
1
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with0
; for each digit, increment the respective counter/flag only if this counter is still0
. At the end writed1 + 2*d2 + 3*d3 ...
Ugly, but will work.
– Stephan Lechner
Nov 23 '18 at 21:18
3
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
7
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
1
1
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
1
1
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with
0
; for each digit, increment the respective counter/flag only if this counter is still 0
. At the end write d1 + 2*d2 + 3*d3 ...
Ugly, but will work.– Stephan Lechner
Nov 23 '18 at 21:18
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with
0
; for each digit, increment the respective counter/flag only if this counter is still 0
. At the end write d1 + 2*d2 + 3*d3 ...
Ugly, but will work.– Stephan Lechner
Nov 23 '18 at 21:18
3
3
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
7
7
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30
|
show 14 more comments
3 Answers
3
active
oldest
votes
This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:
#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}
It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:
unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}
Zero is a digit, and it can be repeated ... (Proof:100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)
– usr2564301
Nov 23 '18 at 21:50
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
add a comment |
I have avoided for loops and arrays and function calls.
Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).
While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.
We only look at each digit one time, so duplicates are ignored.
# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;
printf("Please enter a number: ");
scanf("%d", &input);
//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;
while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}
digit++; // increment the digit
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
add a comment |
A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.
Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.
unsigned SumUniqueDigits(unsigned n) {
unsigned sum = 0;
while (n) {
unsigned lsdigit = n % 10;
n /= 10;
unsigned mssdigits = n;
while (mssdigits) {
if (mssdigits % 10 == lsdigit) {
lsdigit = 0;
break;
}
mssdigits /= 10;
}
sum += lsdigit;
}
return sum;
}
Alternative even faster: On re-examination - failed test code. Removed.
@rici Its right to left.811%8
does not occur, but81%1
and then8%1
. Code tested as functional the same as yours, just a performance difference.
– chux
Nov 24 '18 at 1:28
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:
#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}
It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:
unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}
Zero is a digit, and it can be repeated ... (Proof:100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)
– usr2564301
Nov 23 '18 at 21:50
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
add a comment |
This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:
#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}
It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:
unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}
Zero is a digit, and it can be repeated ... (Proof:100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)
– usr2564301
Nov 23 '18 at 21:50
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
add a comment |
This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:
#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}
It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:
unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}
This is a really dumb way to solve this problem, but it uses neither arrays nor bit vectors:
#include <stdbool.h>
bool numberHasDigit(unsigned n, unsigned digit) {
while (n) {
if (n % 10 == digit) return true;
n /= 10;
}
return false;
}
unsigned sumOfUniqueDigits(unsigned n) {
unsigned sum = 0;
for (unsigned digit = 1; digit <= 9; ++digit) {
if (numberHasDigit(n, digit)) sum += digit;
}
return sum;
}
It's dumb because using an array (or bit vector) of flags is much faster, particularly for big numbers, and the code is just as simple:
unsigned sumOfUniqueDigits(unsigned n) {
bool seen[10] = {false};
unsigned sum = 0;
while (n) {
unsigned digit = n % 10;
if (!seen[digit]) {
sum += digit;
seen[digit] = true;
}
n = n / 10;
}
return sum;
}
edited Nov 24 '18 at 2:23
answered Nov 23 '18 at 21:38
ricirici
158k21139207
158k21139207
Zero is a digit, and it can be repeated ... (Proof:100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)
– usr2564301
Nov 23 '18 at 21:50
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
add a comment |
Zero is a digit, and it can be repeated ... (Proof:100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)
– usr2564301
Nov 23 '18 at 21:50
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
Zero is a digit, and it can be repeated ... (Proof:
100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)– usr2564301
Nov 23 '18 at 21:50
Zero is a digit, and it can be repeated ... (Proof:
100
). (later oh wait, never mind – I am dumb too. Any number of zeros don't matter because they do not take part of the sum in a meaningful way. (Oops.)– usr2564301
Nov 23 '18 at 21:50
1
1
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
I believe the OP said that he is looking for the sum of the digits, so zero would be ignoreed. ie. sum(100) = sum(1000) = 1. Oops. i see that you chagned your comment.
– Gardener
Nov 23 '18 at 21:54
add a comment |
I have avoided for loops and arrays and function calls.
Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).
While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.
We only look at each digit one time, so duplicates are ignored.
# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;
printf("Please enter a number: ");
scanf("%d", &input);
//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;
while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}
digit++; // increment the digit
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
add a comment |
I have avoided for loops and arrays and function calls.
Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).
While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.
We only look at each digit one time, so duplicates are ignored.
# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;
printf("Please enter a number: ");
scanf("%d", &input);
//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;
while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}
digit++; // increment the digit
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
add a comment |
I have avoided for loops and arrays and function calls.
Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).
While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.
We only look at each digit one time, so duplicates are ignored.
# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;
printf("Please enter a number: ");
scanf("%d", &input);
//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;
while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}
digit++; // increment the digit
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
I have avoided for loops and arrays and function calls.
Basically, we run a while loop that looks at the 9 interesting digits (1,2,3,4,5,6,7,8,9).
While looking at each digit, we run another while loop to see if the current digit is in the number by using your modulus and division looping. If the digit is in the number, then we add it to the sum.
We only look at each digit one time, so duplicates are ignored.
# include "stdio.h"
int main()
{
int input = 0;
int sum = 0;
printf("Please enter a number: ");
scanf("%d", &input);
//, now, subtract those values that do not exist in your number
int digit = 1; // start at 1, since 0 does not add to the sum
while (digit < 10) {
// check to see if the digit is in the number
int testInput = input;
while (testInput > 0) {
if (testInput % 10 == digit) {
sum += digit;
break; // Don't test for this digit any more, so go back to the outer loop.
}
testInput /= 10;
}
digit++; // increment the digit
}
printf("Sum of different digits is: %dn", sum);
return 0;
}
edited Nov 23 '18 at 22:02
answered Nov 23 '18 at 21:50
GardenerGardener
1,5921616
1,5921616
add a comment |
add a comment |
A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.
Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.
unsigned SumUniqueDigits(unsigned n) {
unsigned sum = 0;
while (n) {
unsigned lsdigit = n % 10;
n /= 10;
unsigned mssdigits = n;
while (mssdigits) {
if (mssdigits % 10 == lsdigit) {
lsdigit = 0;
break;
}
mssdigits /= 10;
}
sum += lsdigit;
}
return sum;
}
Alternative even faster: On re-examination - failed test code. Removed.
@rici Its right to left.811%8
does not occur, but81%1
and then8%1
. Code tested as functional the same as yours, just a performance difference.
– chux
Nov 24 '18 at 1:28
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
add a comment |
A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.
Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.
unsigned SumUniqueDigits(unsigned n) {
unsigned sum = 0;
while (n) {
unsigned lsdigit = n % 10;
n /= 10;
unsigned mssdigits = n;
while (mssdigits) {
if (mssdigits % 10 == lsdigit) {
lsdigit = 0;
break;
}
mssdigits /= 10;
}
sum += lsdigit;
}
return sum;
}
Alternative even faster: On re-examination - failed test code. Removed.
@rici Its right to left.811%8
does not occur, but81%1
and then8%1
. Code tested as functional the same as yours, just a performance difference.
– chux
Nov 24 '18 at 1:28
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
add a comment |
A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.
Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.
unsigned SumUniqueDigits(unsigned n) {
unsigned sum = 0;
while (n) {
unsigned lsdigit = n % 10;
n /= 10;
unsigned mssdigits = n;
while (mssdigits) {
if (mssdigits % 10 == lsdigit) {
lsdigit = 0;
break;
}
mssdigits /= 10;
}
sum += lsdigit;
}
return sum;
}
Alternative even faster: On re-examination - failed test code. Removed.
A variation on the @rici good answer (and written as a one function call as @John Murray) with reduced operations - on average about 2x faster.
Rather than iterate 1 to 9, it looks for a repeat of the least significant digit in the rest of the number.
unsigned SumUniqueDigits(unsigned n) {
unsigned sum = 0;
while (n) {
unsigned lsdigit = n % 10;
n /= 10;
unsigned mssdigits = n;
while (mssdigits) {
if (mssdigits % 10 == lsdigit) {
lsdigit = 0;
break;
}
mssdigits /= 10;
}
sum += lsdigit;
}
return sum;
}
Alternative even faster: On re-examination - failed test code. Removed.
edited Nov 24 '18 at 3:59
answered Nov 23 '18 at 21:54
chuxchux
85.1k874157
85.1k874157
@rici Its right to left.811%8
does not occur, but81%1
and then8%1
. Code tested as functional the same as yours, just a performance difference.
– chux
Nov 24 '18 at 1:28
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
add a comment |
@rici Its right to left.811%8
does not occur, but81%1
and then8%1
. Code tested as functional the same as yours, just a performance difference.
– chux
Nov 24 '18 at 1:28
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
@rici Its right to left.
811%8
does not occur, but 81%1
and then 8%1
. Code tested as functional the same as yours, just a performance difference.– chux
Nov 24 '18 at 1:28
@rici Its right to left.
811%8
does not occur, but 81%1
and then 8%1
. Code tested as functional the same as yours, just a performance difference.– chux
Nov 24 '18 at 1:28
1
1
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
@rici I had 2 mistakes in my "even faster alternative" 1) in the algorithm itself and 2) in the un-posted test code that failed to detect the error. 2nd approach removed.
– chux
Nov 24 '18 at 4:01
add a comment |
What does "repeated" mean? Same digit next to each other? A digit appearing more then once in the whole number?
– Swordfish
Nov 23 '18 at 21:05
1
For example: 123145... 1 is repeated twice. so the output should be 15 (1+2+3+4+5)
– Ron Segal
Nov 23 '18 at 21:07
1
If you haven't learned arrays, probably you haven't learned about bit arithmetic, too. In such a case, you could define 9 variables, one for each digit, initialized with
0
; for each digit, increment the respective counter/flag only if this counter is still0
. At the end writed1 + 2*d2 + 3*d3 ...
Ugly, but will work.– Stephan Lechner
Nov 23 '18 at 21:18
3
using array is probably faster than with bit masking. Stephan idea would work too. Those assignments are sooo away from real life
– Jean-François Fabre♦
Nov 23 '18 at 21:19
7
"school doesn't want us to use things we didn't learn yet" is totally perverse (and you can quote me on that). The point of a university is to help you learn, not to restrict you from unauthorized learning. If you'd said "school doesn't want us to copy solutions we don't understand," that would be different -- but then they shouldn't be OK with you looking for a solution here.
– rici
Nov 23 '18 at 21:30