Sik
|
|
« Reply #20 on: November 19, 2014, 04:20:38 AM » |
|
That last method is actually used for doing the equivalent of modulo with floating point values, in case you wonder. Also why didn't anybody mention this one this yet? static const int multiples_of_5[] = { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, ... };
int is_multiple_of_5(int value) { int i; for (i = 0; i < sizeof(multiples_of_5) / sizeof(int); i++) { if (value == multiples_of_5[i]) return 1; } return 0; } (that's TDWTF material right there)
|
|
|
Logged
|
|
|
|
Netsu
|
|
« Reply #21 on: November 19, 2014, 05:07:41 AM » |
|
Look up tables for quickness! static const bool multiple_of_5_LUT[] = { true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, ... }
bool is_multiple_of_5(int value) { return multiple_of_5_LUT[value]; }
|
|
|
Logged
|
|
|
|
Sik
|
|
« Reply #22 on: November 19, 2014, 07:09:33 AM » |
|
Sorry, but that takes up too much space. I mean, why spend one byte per value when you can use one bit instead? static const uint8_t multiples_of_5[] = { 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, 0x21, 0x84, 0x10, 0x42, 0x08, ... };
bool is_multiple_of_5(int value) { return multiples_of_5[value >> 3] & 1 << (value & 7) ? true : false; }
|
|
|
Logged
|
|
|
|
Average Software
|
|
« Reply #23 on: November 19, 2014, 06:01:00 PM » |
|
Not that it matters in this case, but modulo and remainder are only the same thing if the numbers involved are positive. When negatives come into play, they're quite different. I've seen very few languages that actually offer remainder, Ada is the only one that comes to mind, but Fortran probably has it too.
|
|
|
Logged
|
What would John Carmack do?
|
|
|
TheLastBanana
|
|
« Reply #24 on: November 19, 2014, 11:59:17 PM » |
|
Are you kidding me? Manually setting up a lookup table is super inefficient from the point of view of coding. If you want to do it properly, use a recursive, memoized approach (this one is in Python). multiple_of_5_memo = {}
def is_multiple_of_5(num): # Handle negatives properly num = abs(num) # Return memoized result if num in multiple_of_5_memo: return multiple_of_5_memo[num]
# Base case if (num < 5): return (num == 0)
# Recurse and memoize result = is_multiple_of_5(num - 5) multiple_of_5_memo[num] = result
return result
|
|
|
Logged
|
|
|
|
Mason_Baumgar
Level 0
|
|
« Reply #25 on: November 23, 2014, 05:13:26 AM » |
|
thank you
|
|
|
Logged
|
Imran Ali
|
|
|
Photon
|
|
« Reply #26 on: November 24, 2014, 05:09:59 PM » |
|
Are you kidding me? Manually setting up a lookup table is super inefficient from the point of view of coding.
Anyway (another Python suggestion)! import math
x = float(num) x /= 5.0 y = math.floor(num) if x == y: return True else: return False
|
|
|
Logged
|
|
|
|
|