DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #157 - Is N Divisible by X and Y?

Create a function isDivisible(n, x, y) that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero digits.

Example:
is_divisible(3,1,3)--> True because 3 is divisible by 1 and 3
is_divisible(12,2,6)--> True because 12 is divisible by 2 and 6
is_divisible(100,5,3)--> False because 100 is not divisible by 3
is_divisible(12,7,5)--> False because 12 is neither divisible by 7 nor 5


This challenge comes from naaz on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (10)

Collapse
 
nijeesh4all profile image
Nijeesh Joshy • Edited

Ruby


require 'minitest'


def is_divisible(num, x, y)
  num % x == 0 && num % y == 0
end

class IsDivisibleTest < MiniTest::Test
  def test_is_divisible
    assert_equal true, is_divisible(12,4,3), "is_divisible(12,4,3) should return a boolean true"
    assert_equal false, is_divisible(100,4,3), "is_divisible(100,4,3) should return a boolean false"
    assert_equal true, is_divisible(0,4,3), "is_divisible(0,4,3) should return a boolean true"
    assert_equal false, is_divisible(10,0,3), "is_divisible(10,0,3) should return a boolean false"
  end
end

MiniTest.run
Collapse
 
nickyoung profile image
Nick Young • Edited

Trick question. In the instructions, it says to make a function called isDivisible() but in the example, it uses is_divisible() 🤔

PHP:

function isDivisible($n, $x, $y) {
    return ( $n % $x === 0 ) && ( $n % $y === 0 );
}
Collapse
 
savagepixie profile image
SavagePixie

JavaScript

const isDivisible = (n, x, y) => n % x === 0 && n % y === 0
Collapse
 
mistrikushal profile image
Kushal

ruby

def isDivisible(n, x, y)
  (n % x == 0) && (n % y == 0)
end
Collapse
 
dynamicboy24 profile image
DynamicBoy24

No, Bro, it's not working.

Collapse
 
aminnairi profile image
Amin

Haskell

isDivisibleBy :: Int -> Int -> Int -> Bool
isDivisibleBy x y n = all ((==) 0 . mod n) [x, y]

Test.

Collapse
 
dynamicboy24 profile image
DynamicBoy24

No bro it's not working
Failed to detect module name:
isDivisibleBy :: Int -> Int -> Int -> Bool
isDivisibleBy x y n = all ((==) 0 . mod n) [x, y]

Collapse
 
aminnairi profile image
Amin

Hi and thanks for your comment.

Have you tried the test link?

Collapse
 
avalander profile image
Avalander

Racket

(define (is-divisible n x y)
  (and
   (= 0 (modulo n x))
   (= 0 (modulo n y))))
Collapse
 
dynamicboy24 profile image
DynamicBoy24

No, it's not working.
default-load-handler: expected a `module' declaration, but found something else
file: /workspace/racket/solution.rkt
context...:
default-load-handler
standard-module-name-resolver
perform-require!78
for-loop
finish
[repeats 1 more time]
pass-1-and-2-loop
module-begin-k
expand-module16
expand-capturing-lifts
temp118_0
temp91_0
compile15
temp85_0
standard-module-name-resolver
module-path-index-resolve
...

Some comments may only be visible to logged-in visitors. Sign in to view all comments.