|
All integers evenly divisible by 5 must end in either 5 or 0 when expressed in base 10. This is basic math.
Software Zen: delete this;
|
|
|
|
|
Is this discussion really taking place? What is this, 3rd grade?
ken@kasajian.com / www.kasajian.com
|
|
|
|
|
Kenneth Kasajian wrote: Is this discussion really taking place? What is this, 3rd grade?
What is everones problem?
I asked how to solve a basic math based problem PROGMATICALLY as I am a beginner when it comes to c#, and I am wishing I never bothered asking!
Although, there are a number of people who have offered working solutions.
Why must people post such a responce as yours? Did your post really help in anyway?
Regards,
Stephen
|
|
|
|
|
Let me be clear. I don't have any problem with people asking very basic programming questions. In fact, I encourage it. I don't expect people to know everything about programming.
I was commenting on this discussions of what numbers are divisible by 5, as in:
"However, I was under the impression I could generate numbers that would be divisible by 5, but not end in 5?
Is this wrong?"
You're kidding me right? You don't know that the numbers 10 and 20 are divisible by 5? Clearly they don't end in 5.
My comment was no to the programming question.
ken@kasajian.com / www.kasajian.com
|
|
|
|
|
Kenneth Kasajian wrote: You're kidding me right?
Are you?
This thread was cleared up a while ago, and I am not going to explain myself yet again
Regards!
|
|
|
|
|
Interesting. So you're responding to say that you're not going to respond. Just don't bother dude. LOL
ken@kasajian.com / www.kasajian.com
|
|
|
|
|
Seriously??? Were you sleeping during elementary math classes? I'm sorry but if I were your boss and you asked me that question I would transfer you out of my department over to HR or maybe Daycare.
|
|
|
|
|
RDSchaefer wrote: Seriously??? Were you sleeping during elementary math classes? I'm sorry but if I were your boss and you asked me that question I would transfer you out of my department over to HR or maybe Daycare.
This is the last time I will even bother with a reply to the outright rude comments!
If you were to bother yourself to read all of the posts, you would realise that I already appologised that I had not explained myself properly, and my problem is not with math.
I am getting fed up with having to explain this, and to be honest, it is getting hard to hold my tounge with the likes of you!
Your not my boss, are you? So why not keep your ignorant comments to yourself.
|
|
|
|
|
I was not commenting on your coding skills and I did read all the posts, however I do apologize. I was just so astounded to think someone could actually not know that (Any number ending in 5 or 0 is divisible by 5), that I reacted in a knee-jerk fashion. I also didn't solve your problem because it had already been well answered.
BTW, you really need to develop thicker skin. There will always be some people who know so much more than you that they sometimes get irritated answering the same, in their opinion, dumb questions over and over again. It still happens to me occasionally.
|
|
|
|
|
That’s OK, I was already upset by someone else's comment, which is why I reacted the way I did.
RDSchaefer wrote: BTW, you really need to develop thicker skin.
Actually, I have a thick skin. Growing up with the surname "Darling" ensures that you develop one quickly
I am a little tired today, and when I received a comment from someone on here telling me that I was "NOT a scientist" when they don’t know me, and have never spoke to me, was what made me flip a little.
I can understand that it happens, and normally it would be water off a ducks back, however, on this occasion it caused me to respond.
Kind Regards,
Stephen
|
|
|
|
|
you shouldn't add 5 or 10 but 5 or 0.
In the case the random generator provides 9999 then you would get
9999 * 10 + 10 = 99990 + 10 = 100000
six digits
|
|
|
|
|
This solution is not correct - it exhibits a possible overflow error into 6 digits.
Consider: 4 Random digits all come out as 9 (multiply by 10 = 99990), then add 10 you get 100000 which does not meet the requirements...
The correct solution is:
Generate a four digit random number
Multiply it by 10 (to shift into correct position)
Then randomly choose to make the last digit a 5 or a 0 (adding 10 is the incorrect step)
The result is divisible by 5.
Regards
David.
|
|
|
|
|
See my correction - you don't need to add 10 at all (you can add 5 without fear).
That was a silly on my part.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
|
David1987 wrote: And make sure you reuse a single instance of Random ,
How exactly do I ensure that I am doing this?
I am using rand a number of times, and although I get different values, it does appear that they are very simular.
Regards,
Stephen
|
|
|
|
|
Is this a joke question? Generate a four digit number and add an extra number to the end, being either 5 or 0. Then don't check it because it will be correct!!!
e.g. 4678; add 5 on the end to give 46785.
|
|
|
|
|
Exactly my take on it
|
|
|
|
|
Random random = new Random();
return (random.Next(17999) + 2000) * 5;
this gives a number divisable by 5 between 10000 and 99995
Paulo Gomes
Over and Out
|
|
|
|
|
Generate a five digit random number, convert to string, replace the right most digit with 5 (or 0), parse the string back to an int.
No need to check as anything ending in 5 or 0 is divible by 5.
|
|
|
|
|
This would work as an alternative but the math approach is better (more performant) unless you want the final value to be a string. And, even then the math approach would be faster.
(nnnn*10)=nnnn0
It's the same as appending a "0" to a four-digit number but it's much faster.
|
|
|
|
|
I'm sorry but a lot of the answers made me laugh...
By definition any number that has been multiplied by 5 is divisible by 5. So the solution is:
x=random(low_limit, high_limit)*5; no need to check this!
The five digit condition is a little bit more complicated, because it is not clearly defined. is 00005 a five digit number by this definition? Low limit will then be 0, high limit 19999 (the highest 5 digit number fullfilling the condition is 99995).
To get a number that is divisable by 5, the ending needs to be 0 or 5. If you need checking, the modulo division is correct.
|
|
|
|
|
// To generate as random numbers as possible, this variable must only
// be initialized once and then reused as much as possible.
Random rand = new Random();
/// <summary>
/// Generates a random 5 digit number that is always divisible by 5.
/// </summary>
/// <returns>Returns a random integer 5 digits long that is evenly divisible by 5.</returns>
private int GetDivBy5()
{
// rnd.Next(minValue, maxValue) returns a random between minValue
// and maxValue, inclusive. Use min/max values of 1000 and 9999 to
// yield a random 4 digit number between 1000 and 9999 then multiply
// by 10 to get a 5 digit number. This number will always be
// divisible by 5 but will always end in 0.
int divBy5 = rand.Next(1000, 9999) * 10;
// To get more random and double the possible result set, randomly
// add on 5. This will yield the total possible set of integers
// between 10000 and 99995 that are divisible by 5.
if (rand.NextDouble() < 0.5)
return divBy5;
else
return divBy5 + 5;
}
Stephen, you mention you are a coding noobie. So, a short word about random number generators - they're not really very random.
All they do is take a seed (beginning) value and run it through a mathmatical algorithm that generates a new number. If you use the same seed value every time, you will get the same "random" number every time. Most random number generators can either self-seed - generally using the current clock value - or you can pass them a seed value when you initialize them. This is why you only want to initialize your random number generator once and reuse it over and over.
Once you get past that, the rest is pretty easy. Just use the math several others have already provided. See the comments in the code...
<edit>
Um, just looked at the thread in the other forum where you defined your requirements a little better. It seems you would like to include 0-9999 in your result set too. You can do this easily enough by changing the min value passed to rand.Next(minValue, maxValue) to 0 instead of 1000. This last bit, is a bit tricky in that in programming terms "00005" is actually not a number but is really a string. To get the leading zeros, you need to convert your final value to a string and use a formatting mask to append the leading zeros. This also assumes "00000" is valid. If it's not, test for it with an "if" and call GetDivBy5 again.
So, to get a string with leading zeros use the following;
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.GetDivBy5().ToString("00000");
}
</edit>
modified on Tuesday, August 23, 2011 11:03 AM
|
|
|
|
|
Thank you very much.
Sorry I missed this post, and to be honest I am a little p***** off with the negative comments I am receiving, that is why I stopped looking at the responses as it upset me a little that people on here can be so damn rude and insulting, oh, and judgmental, for no apparent reason?
Anyway, thank you very much.
Kind Regards,
Stephen
|
|
|
|
|
Everyone's a Legend in Their Own Lunchtime. You've learned a lesson - whether you wished to or not - about being precise in framing your question, so I hope that at least that, and the sensible answers you did get, helped.
As to the others who felt it was easier to ridicule than to stop for a second and realise there was more to it, I apologise.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Chris Maunder wrote: Everyone's a Legend in Their Own Lunchtime.
Agreed!
Chris Maunder wrote: You've learned a lesson - whether you wished to or not
Actually I learned two. Yes; one was indeed to think allot more before I pose my question!
Chris Maunder wrote: As to the others who felt it was easier to ridicule than to stop for a second and realise there was more to it, I apologise.
You have nothing to apologise for, and it was really just the one statement that I was not a scientist that caused me to react.
Never mind, I have moved on
Kind Regards,
Stephen
|
|
|
|