Even sum of digits

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 29
Reputation: FREEZX is an unknown quantity at this point 
Solved Threads: 0
FREEZX FREEZX is offline Offline
Light Poster

Even sum of digits

 
0
  #1
Apr 10th, 2009
I need to write a program that will count how many numbers within an interval have an even sum of digits. you are given two numbers, a and b. i wrote a little program that is a little slow.. it checks for the sum of the digits for every number in the interval. Any suggestions about a better algorithm appreciated.
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. string c;
  8. stringstream ss;
  9. int a,b, rezx, count;
  10. cin>>a>>b;
  11. for(int i=a; i<b; i++)
  12. {
  13. ss<<i;
  14. c=ss.str();
  15. ss.str("");
  16. for(int x=0; x<c.length(); x++)
  17. {
  18. rezx=rezx+c[x];
  19. }
  20. if(rezx%2==0)
  21. {
  22. count++;
  23. }
  24. rezx=0;
  25. }
  26. cout<<count-1;
  27. system("pause");
  28. return 0;
  29. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
1
  #2
Apr 10th, 2009
I think the following algorithm is a better one:

If you know the starting number of the interval:
-> Check whether the sum of it's digits are even (you could use the modulo operator for this)
-> If the sum was even, you automatically know the next number's sum of digits is odd

e.g: 450 => odd ; 451 => even ; 452 => odd ; 453 => even ; etc. ...

Edit:: So actually you don't have to check each number's sum of digits

Hope this helps !
Last edited by tux4life; Apr 10th, 2009 at 6:59 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 29
Reputation: FREEZX is an unknown quantity at this point 
Solved Threads: 0
FREEZX FREEZX is offline Offline
Light Poster

Re: Even sum of digits

 
0
  #3
Apr 10th, 2009
yeah but what when you get to a number like 159... 1+5+9=15 which is odd. than the next one 160 its odd again.. how do i get over these?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #4
Apr 10th, 2009
The method described here is probably a more efficient one as yours as it isn't converting the number to a string each time ...

Edit:: I have to admit my previous post was a bad one ...
Last edited by tux4life; Apr 10th, 2009 at 7:56 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Even sum of digits

 
1
  #5
Apr 10th, 2009
It's not only the most slow algorithm (the original one) but it's a wrong algorithm. You have got not a sum of digits but a sum of char codes of digits. It's the other story that char code of '0' is even numbert in ASCII table; the C language does not specify this fact.

It's so easy to get the last digit of integer n: n%10 then get the last digit of n /= 10 and so on...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 29
Reputation: FREEZX is an unknown quantity at this point 
Solved Threads: 0
FREEZX FREEZX is offline Offline
Light Poster

Re: Even sum of digits

 
0
  #6
Apr 10th, 2009
should i try to make an algorithm that checks if the first number's sum of digits is even and than to count the interval until 9 is reached and than to switch? example:
289-false
290-false
291-true
292-false
293-true
....
299-true
300-false
and than to count the true ones
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #7
Apr 10th, 2009
Originally Posted by FREEZX View Post
should i try to make an algorithm that checks if the first number's sum of digits is even and than to count the interval until 9 is reached and than to switch? example:
289-false
290-false
291-true
292-false
293-true
....
299-true
300-false
and than to count the true ones
No, you only have to write a function which calculates the sum of the digits of a number (using the information mentioned in my previous post (look at the link) and ArkM's post) ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Even sum of digits

 
0
  #8
Apr 10th, 2009
I don't understand - why do you have the numbers input as 'int'? Why don't you have the numbers input directly into a string and then use the operator [] to move through the digits and count values? I think it could be much more efficient - just run a loop like for(index=0;index<number.length();index++) and add every digit to a variable that holds the sum. If you assume ASCII, you won't even need to convert the character digits into digits, because their ASCII values are even for even digits.
Last edited by unbeatable0; Apr 10th, 2009 at 10:01 am. Reason: Typo
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Even sum of digits

 
0
  #9
Apr 10th, 2009
Originally Posted by unbeatable0 View Post
I don't understand - why do you have the numbers input as 'int'? Why don't you have the numbers input directly into a string and then use the operator [] to move through the digits and count values? I think it could be much more efficient - just run a loop like for(index=0;index<number.length();index++) and add every digit to a variable that holds the sum. If you assume ASCII, you won't even need to convert the character digits into digits, because their ASCII values are even for even digits.
I actually don't understand why you need a string for that purpose, you can do it using mathematical operations only.
Why making it difficult if it can be easy ?

Look at ArkM's post !
Last edited by tux4life; Apr 10th, 2009 at 10:11 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Even sum of digits

 
0
  #10
Apr 10th, 2009
Originally Posted by tux4life View Post
I actually don't understand why you need a string for that purpose, you can do it using mathematical operations only.
Why making it difficult if it can be easy ?

Look at ArkM's post !
When you don't need to do calculations with the number there's no reason for doing the mathematical operations on it - you have the possibility to separate the characters more easily usign strings.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC