
Problem with Logic
I have a logic problem.<BR><BR>If every multiple of 17, 18, 19, do so and so ...<BR><BR>Eg, <BR>if a > 16 and a < 20 then <BR> ....<BR>end if<BR><BR>if a > 32 and a < 40 then<BR> ...<BR>end if<BR><BR>* the end of the eg.<BR><BR>If I put my code like above, it will be a hardcode type. How my logic should be for this case? Thanks.

Think about it:
Queston 1: What is true of any multiple of 17 when you divide it by 17?<BR><BR>Question 2: What is true of every number that is *NOT* a multiple of 17 when you divide it by 17?<BR><BR>Questions 3 through 6: Repeat questions 1 and 2 for the number 18 and 19.<BR><BR>Think about this before you look at my answer.<BR><BR><BR>

No peeking!
Answer to 1 and 2: If you divide a number by 17 and the result is an INTEGER, then the number was a multiple of 17.<BR><BR>So:<BR><BR><%<BR>If a/17 = a17 OR a/18 = a18 OR a/19 = a19 Then<BR> Response.Write "Number is a multiple of 17, 18, or 19"<BR>Else<BR> Response.Write "Number is NOT a multiple of 17, 18, or 19"<BR>End If<BR>%><BR><BR>Why does that work? Because the backslash operator in VBScript does an INTEGER ONLY division.<BR><BR>Basically, a17 is the same as writing Int(a/17).<BR><BR>So what we are saying is<BR> If a/17 = Int(a/17) Then ...<BR><BR>In other words, do two steps: <BR> divide a by 17 <BR> check to see if that number is the same as the INT of itself<BR><BR>Example:<BR> 51 / 17 = 3.0<BR> So 51/17 = Int(51/17) <BR> because 3.0 = Int(3.0)<BR>But <BR> 52 / 17 = 3.058...<BR> And Int(3.058) is just 3, so<BR> Int(3.058) <> 3.058<BR><BR>And, once again, writing <BR> 51/7 = 517<BR>is just shorthand for<BR> 51/7 = Int(51/7)<BR><BR>Hokay?<BR><BR>

RE: No peeking!
can't you employ 'mod' here? <BR><BR>if x mod y = 0 then<BR>' x is a multiple of y<BR>end if

Sure...I just figured...
...that maybe understanding <BR> a/17 = Int(a/17) <BR>might be easier.<BR><BR>But sure, you could do<BR> If a MOD 17 = 0 OR a MOD 18 = 0 OR a MOD 19 = 0 Then<BR><BR>[It's actually a tad faster, since the MOD operator takes the same amount of time as the backslash operator, so you eliminate the other divide.]<BR><BR>Now YOU get to explain what MOD means. <grin/><BR><BR><BR><BR>

RE: Sure...I just figured...
> Now YOU get to explain what MOD means. <BR><BR>no problem :<BR><BR>http://msdn.microsoft.com/library/default.asp?url=/library/enus/script56/html/vsoprmod.asp<BR><BR>;)

RE: No peeking!
I did thought about it after your questions. Anyway, thanks for your comprehensive answer. Not only you just provide just the answer but prompt me to think about it first. <BR><BR>Atrax, your idea is briliant. I will try it out as well. Thanks guys.

Grumble...I should have...
...thought of that. Two points for you.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

