Skip to content

#116 Divides Evenly

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

116 Divides Evenly

Given two integers, a and b, return true if a can be divided evenly by b.

Return false otherwise.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/116 Divides Evenly.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Given two integers, a and b, return true if a can be divided evenly by b.
//Return false otherwise.
function dividesEvenly(a, b) {
    return a % b === 0;
}
import { assertEquals } from '../testHelper.js';
assertEquals(dividesEvenly(98, 7), true)
assertEquals(dividesEvenly(87, 49), false)
assertEquals(dividesEvenly(34, 14), false)
assertEquals(dividesEvenly(78, 6), true)
assertEquals(dividesEvenly(30, 4), false)
assertEquals(dividesEvenly(87, 73), false)
assertEquals(dividesEvenly(74, 7), false)
assertEquals(dividesEvenly(87, 29), true)
assertEquals(dividesEvenly(48, 24), true)
assertEquals(dividesEvenly(99, 20), false)
assertEquals(dividesEvenly(98, 49), true)
assertEquals(dividesEvenly(100, 6), false)
assertEquals(dividesEvenly(64, 4), true)
assertEquals(dividesEvenly(70, 35), true)
assertEquals(dividesEvenly(38, 38), true)
assertEquals(dividesEvenly(29, 3), false)
assertEquals(dividesEvenly(20, 8), false)
assertEquals(dividesEvenly(66, 50), false)
assertEquals(dividesEvenly(95, 1), true)
assertEquals(dividesEvenly(58, 2), true)

Historical reference notes

Cross-language explanation preserved from the study materials

Reference file: #031 Divides Evenly.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.

# [Divides Evenly](https://edabit.com/challenge/ghJ7G5jtKprtjfep2)
Published by [Sover David](https://edabit.com/user/az4cHcNAEbzZwEdpR)

math numbers validation

Given two integers, `a` and `b`, return `true` if `a` can be divided evenly by `b`. Return `false` otherwise.

```
dividesEvenly(98, 7) ➞ true
// 98/7 = 14
```

```
dividesEvenly(85, 4) ➞ false
// 85/4 = 21.25
```

`a` will always be greater than or equal to `b`.

### Time Complexity:
The time complexity of `dividesEvenly` is _O(1)_. The function performs a single modulus operation (`a % b`), which completes in constant time, regardless of the values of `a` and `b`.

### Space Complexity:
The space complexity is also _O(1)_. The function only requires space for the input parameters (`a` and `b`) and the return value (a boolean). No additional space is used that varies with the input size.
![flowchart](https://diagrams.helpful.dev/d-r2/d-r2:aEq8f24z)

//Given two integers, a and b, return true if a can be divided evenly by b. Return false otherwise.
using BenchmarkDotNet.Attributes;
using System;

namespace Challenges
{
    public class Program31b
    {
        public static bool DividesEvenly(int a, int b) => a % b == 0;
    }
    public class BenchmarkProgram31b
    {
        [Benchmark]
        [Arguments(98, 7)]
        [Arguments(87, 49)]
        [Arguments(34, 14)]
        [Arguments(78, 6)]
        [Arguments(30, 4)]
        [Arguments(87, 73)]
        [Arguments(74, 7)]
        [Arguments(87, 29)]
        [Arguments(48, 24)]
        [Arguments(99, 20)]
        [Arguments(98, 49)]
        [Arguments(100, 6)]
        [Arguments(64, 4)]
        [Arguments(70, 35)]
        [Arguments(38, 38)]
        [Arguments(29, 3)]
        [Arguments(20, 8)]
        [Arguments(66, 50)]
        [Arguments(95, 1)]
        [Arguments(58, 2)]
        public bool DividesEvenly(int a, int b) => Program31b.DividesEvenly(a, b);
    }
}

using System;
using Challenges.Edabit;
using NUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class Tests31b
    {
        [Test]
        [TestCase(98, 7, true)]
        [TestCase(87, 49, false)]
        [TestCase(34, 14, false)]
        [TestCase(78, 6, true)]
        [TestCase(30, 4, false)]
        [TestCase(87, 73, false)]
        [TestCase(74, 7, false)]
        [TestCase(87, 29, true)]
        [TestCase(48, 24, true)]
        [TestCase(99, 20, false)]
        [TestCase(98, 49, true)]
        [TestCase(100, 6, false)]
        [TestCase(64, 4, true)]
        [TestCase(70, 35, true)]
        [TestCase(38, 38, true)]
        [TestCase(29, 3, false)]
        [TestCase(20, 8, false)]
        [TestCase(66, 50, false)]
        [TestCase(95, 1, true)]
        [TestCase(58, 2, true)]

        public void FixedTest(int a, int b, bool expectedResult)
        {
            // Arrange
            int dividend = a;
            int divider = b;
            bool result = Program31b.DividesEvenly(dividend, divider);
            Assert.That(result, Is.EqualTo(expectedResult));
        }
    }
}

Clone this wiki locally