Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 24, 2024, 10:51:42 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[GML] How to check if a number is divisible by 5?
Pages: 1 [2]
Print
Author Topic: [GML] How to check if a number is divisible by 5?  (Read 9230 times)
Sik
Level 10
*****


View Profile WWW
« 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?

Code:
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
Level 10
*****


proficient at just chillin'


View Profile WWW
« Reply #21 on: November 19, 2014, 05:07:41 AM »

Look up tables for quickness!

Code:
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
Level 10
*****


View Profile WWW
« 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?

Code:
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
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #23 on: November 19, 2014, 06:01:00 PM »

The "remainder" operation is called modulo, and it's usually present as either mod or "%" (percent) in just about every programming language ever. See http://en.wikipedia.org/wiki/Modular_arithmetic

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
Level 9
****



View Profile WWW
« 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).

Code:
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
*


View Profile
« Reply #25 on: November 23, 2014, 05:13:26 AM »

thank you
Logged

Imran Ali
Photon
Level 4
****


View Profile
« 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.
Hand Clap Huh?

Anyway (another Python suggestion)!

Code:
import math

x = float(num)
x /= 5.0
y = math.floor(num)
if x == y:
    return True
else:
    return False
Logged
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic