Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

num_divisors

By Daniel · December 26, 2008

Computes how many unique natural numbers divide evenly into the argument

// num_divisors(n, exclude)
// Computes how many unique natural numbers divide evenly into the argument
// n: the number to check
// exclude: whether to exclude 1 and n from the set of divisors

var n, exclude, divs, d;

n = argument0;
exclude = argument1;
divs = 0;

// If there is a divisor d < n, there must be a corresponding d > n
for (d = 1; d * d < n; d += 1)
  if (n mod d == 0)
    divs += 2;

// If root n is a divisor, count that only once
if (d * d == n)
  divs += 1;

if (exclude)
  divs -= 2;

return divs;

Categories: Mathematics

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement