# Daily Challenge Post #20 - Number Check

Create a function which checks a number for three different properties.

Is the number prime?
Is the number even?
Is the number a multiple of 10?

Each should return either true or false, which should be given as an array.

Today's challenge comes from sohilpandya on Codewars.

Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

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

### Discussion  David Wickes

Well, as this isn't the most challenging challenge, I decided to challenge myself ;)

Here is an answer in the stack-based programming language Factor.

``````! is an integer even?
: even? ( int -- ? )
2 mod 0 = ;

! is an integer a multiple of ten?
: multiple-of-ten? ( int -- ? )
10 mod 0 = ;

! helper word (that's a 'function' in factor) for prime?
: (prime?) ( int m -- ? )
2dup <= [ t 2nip ] [
2dup mod 0 = [ f 2nip ] [ 1 + (prime?) ] if
] if ;

! is an integer prime?
: prime? ( int -- ? )
2 (prime?)

! all three tests applied to a single integer, output as an array
: all-three ( int -- seq )
2dup
[ prime? ] [ even? ] [ multiple-of-ten? ]
tri* 3array ;

5 all-three
!
! --- Data stack:
! { t f f }
clear
10 all-three
!
! --- Data stack:
! { f t t }
``````

I've never tried anything like this before, so I'd be interested in some feedback if any is available.

Conversely, if anyone would like me to explain what on earth is going on above, please ask and I'll do my best. I really enjoyed writing it. Corey Alexander

Rust Solution!

Went with the naive algorithm for testing for a prime number. Could always improve on that if needed :shruggy:

My test cases for these are also getting thinner and thinner as we go on...

``````fn is_prime(num: u64) -> bool {
if num == 0 {
return false;
}

for i in 2..num {
if num % i == 0 {
return false;
}
}

true
}

pub fn number_property(i: i64) -> (bool, bool, bool) {
let is_even = i % 2 == 0;
let is_divisible_by_ten = i % 10 == 0;

let is_prime = if i > 0 { is_prime(i as u64) } else { false };

(is_prime, is_even, is_divisible_by_ten)
}

#[cfg(test)]
mod tests {
use crate::number_property;

#[test]
fn it_works_for_the_examples() {
assert_eq!(number_property(7), (true, false, false));
assert_eq!(number_property(10), (false, true, true));
}

#[test]
fn it_works_for_negative_examples() {
assert_eq!(number_property(-7), (false, false, false));
assert_eq!(number_property(-10), (false, true, true));
}
}
`````` E. Choroba

Perl solution, using the modulo operator.

``````#!/usr/bin/perl
use warnings;
use strict;

sub prime_even_10 {
my (\$x) = @_;
return \$x > 1 && !(grep 0 == \$x % \$_, 2 .. sqrt \$x),
0 == \$x % 2,
0 == \$x % 10
}

use Test::More tests => 11;

is_deeply [prime_even_10(0)],  [!1,  1,  1];
is_deeply [prime_even_10(1)],  [!1, !1, !1];
is_deeply [prime_even_10(2)],  [ 1,  1, !1];
is_deeply [prime_even_10(3)],  [ 1, !1, !1];
is_deeply [prime_even_10(4)],  [!1,  1, !1];
is_deeply [prime_even_10(5)],  [ 1, !1, !1];
is_deeply [prime_even_10(6)],  [!1,  1, !1];
is_deeply [prime_even_10(7)],  [ 1, !1, !1];
is_deeply [prime_even_10(8)],  [!1,  1, !1];
is_deeply [prime_even_10(9)],  [!1, !1, !1];
is_deeply [prime_even_10(10)], [!1,  1,  1];
`````` peter279k

Here is my simple solution with Python:

``````import math

def is_prime(n):
if n == 2:
return True
if n == 1 or n <= 0:
return False
number = 2
end_number = math.sqrt(n)

while number <= end_number:
if n % number == 0:
return False
number += 1

return True

def is_even(n):
return n % 2 == 0

def is_multiple_ten(n):
return n % 10 == 0

def number_property(n):

`````` official_dulin

Go:

``````package kata

func NumberProperty(n int) []bool {
return []bool{isPrime(n), isEven(n), multipleOfTen(n)}
}

func isPrime(n int) bool {
for i := 2; i < n; i ++ {
if n % i == 0 {
return false
}
}
return n > 1
}

func isEven(n int) bool {
return n % 2 == 0
}

func multipleOfTen(n int) bool {
return n % 10 == 0
}
`````` Oleksii Filonenko

Elixir:

``````defmodule NumberCheck do
require Integer

@spec check(number) :: {prime :: boolean, even :: boolean, multiple_of_ten :: boolean}
def check(number), do: {prime?(number), Integer.is_even(number), rem(number, 10) == 0}

@spec prime?(number) :: boolean
defp prime?(n) when n < 2, do: false
defp prime?(n), do: not Enum.any?(2..(n |> :math.sqrt() |> floor()), &(rem(n, &1) == 0))
end
`````` K.V.Harish

My solution in js

``````const numberCheck = (num) => {
return [`isPrime: \${(() => {
const sqrtOfNum = Math.sqrt(num);
for(let index = 2; index <= sqrtOfNum; index++) {
if(num % index === 0) {
return false;
}
}
return num > 1;
})()}`,
`isEven: \${num % 2 === 0}`,
`isMultipleOfTen: \${num % 10 === 0}`];
};
`````` Jacob Evans

I made the answer earlier and forgot to put it here after I solved it in CodeWars lmao
JavaScript answer, Big O time complexity of O(sqrt(n)) to find if N is a Prime Number.

``````const isPrime = num => {
for(let i = 2, s = Math.sqrt(num); i <= s; i++)
if(num % i === 0) return false;
return num > 1;
}

const numberProperty = n =>  [
isPrime(n),
n % 2 === 0,
n % 10 === 0,
];
`````` Kasper Meyer

Ruby 2.6

``````require "minitest/autorun"
require "prime"

class ArbitraryPropertyNumber
def initialize number
@number = number
end

def prime?
Prime.prime?(@number)
end

def even?
@number.even?
end

def multiple_of_ten?
@number % 10 == 0
end
end

class ArbitraryPropertyNumberValidator
def initialize number
@number = number
end

def validate
[number.prime?, number.even?, number.multiple_of_ten?]
end

private

def number
ArbitraryPropertyNumber.new(@number)
end
end

class ArbitraryPropertyNumberValidatorTest < MiniTest::Test
def test_first_element_is_true_when_number_is_prime
assert_equal [true, false, false], ArbitraryPropertyNumberValidator.new(3).validate
end

def test_second_element_is_true_when_number_is_even
assert_equal [false, true, false], ArbitraryPropertyNumberValidator.new(4).validate
end

def test_third_element_is_true_when_number_is_multiple_of_ten
assert_equal [false, true, true], ArbitraryPropertyNumberValidator.new(20).validate
end
end

``````