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 ! :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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 ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
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) ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
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 ! :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
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.
Not agreed, using strings is more processor intensive, dealing with numbers is actually a speed boost ...
BTW, You can get the last digit of a number using the modulo operator like this: lastdigit = number % 10;
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Running a loop for such a purpose will look like this using mathematical operations:
unsigned short int a,Sum=0;
a=12522;
while(a)
{
Sum+=a%10;
a/=10;
}
Using string operations:
string a;
unsigned short int Sum=0,i;
a="12522";
for(i=0;i<a.length();i++) Sum+=a[i];
I personally prefer the second option when you assume ASCII and don't need a speedy algorithm - for example in this program. It looks clearer for me - more self-explanatory.
It doesn't change much, however. It doesn't worth an argument.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
Using string operations:
string a;
unsigned short int Sum=0,i;
a="12522";
for(i=0;i<a.length();i++) Sum+=a[i];
This code isn't returning the sum of the digits in the number !!
And that's the code you prefer ? Code which isn't working ??!!
Dear unbeatable, to make your code work change it to the following:
string a;
unsigned short int Sum=0,i;
a="12522";
for(i=0;i<a.length();i++) Sum+=a[i]-'0';
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
It's working for determining if the sum is even or not. The Sum mod 2 remains the same.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
It's working for determining if the sum is even or not. The Sum mod 2 remains the same.
Can you explain we howit (your example using strings) was working ??
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
ASCII '0' is 48, '1' is 49... '9' is 57 - '0' mod 2 remains 0, '1' mod 2 remains 1, ... , '9' mod 2 remains 1.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
Just look at this post and you'll see you've made a mistake in your code ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Nope. It doesn't change the answer of Sum%2 simply because '0' mod 2 = 0.
If x=y (mod n), and z=0 (mod n), x+z will still equal y (mod n).
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
You can try if you don't believe me but this code:
#include <iostream>
using namespace std;
int main()
{
unsigned short int a,Sum=0;
a=12522;
while(a)
{
Sum+=a%10;
a/=10;
}
cout << Sum << endl;
}
Has another output than this code:
#include <iostream>
using namespace std;
int main()
{
string a;
unsigned short int Sum=0,i;
a="12522";
for(i=0;i<a.length();i++) Sum+=a[i];
cout << Sum << endl;
}
Convinced ?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
We're talking about the result of the sum modulo 2. The result is the same.
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13