# MuleSoft DataWeave Practice: Prime Number Code

### We learn how to write DataWeave code to check if an input number is prime or composite, sharpening your DataWeave skills in MuleSoft and also boost your logical thinking.

Join the DZone community and get the full member experience.

Join For Free**What is a Prime Number?**

A prime number is a natural number that is greater than 1 and divisible by 1 and itself. That means the prime number will have only two factors 1 and that number itself. Some of the prime numbers are 2, 3, 5, 7,11,13, etc.

Before writing the code let's list down what will be the different types of inputs. So the inputs can be

- 0.
- 1.
- Prime numbers from number 2 onwards.
- Composite numbers like 4, 6, 8, 10, etc.

So we need to handle all the above cases while writing our code. Let's write the code step by step.

**Step 1**

Check if the number is divisible by the numbers between 2 to number-1 (inclusive of both 2 and number-1). For this, we will use:

- Custom Function (Normal Function or Lambda Function). Here we will use the lambda function.
`map`

`dw::Core`

module of DataWeave.`mod`

operator to check if the number is completely divisible by the divisor or not.

**Input:**

`xxxxxxxxxx`

`7`

**Code: **

`xxxxxxxxxx`

`%dw 2.0`

`output application/json`

`var isDivisible = (num) -> `

` (2 to num-1 map (((num mod $) == 0)))`

`---`

`isDivisible(7)`

** **

**Output:**

`xxxxxxxxxx`

`[`

` false,`

` false,`

` false,`

` false,`

` false`

`]`

Let's check the output for payload as `6`

:

**Input:**

`xxxxxxxxxx`

`6`

** **

**Output: **

`xxxxxxxxxx`

`[`

` true,`

` true,`

` false,`

` false`

`]`

Let's understand what we achieved till now with the help of the above two cases:

**payload =**`7`

- We wrote lambda function
`isDivisible`

- We achieved looping functionality using the
`map`

function. We started our loop from 2 to 7-1 i.e., 6. - We used the
`mod`

operator to check if the remainder is zero or not. If the (number mod divisor) is zero then it will return true to the array otherwise it will return false. - As 7 is not divisible by 2,3,4,5,6 it returned the array
`[false,false,false,false,false]`

.

- We wrote lambda function
**payload =**`6`

- We achieved looping functionality using the
`map`

function. We started our loop from 2 to 7-1, i.e., 6. - We used the
`mod`

operator to check if the remainder is zero or not. If the number mod divisor is zero then it will return true to the array otherwise it will return false. - As 6 is divisible by 2, 3 and indivisible by 4 and 5 it returned the array
`[true,true,false,false]`

.

- We achieved looping functionality using the

So, if the number is divisible by any divisor between 2 to number -1 (inclusive both) then returned array will have true as a boolean value for that divisor.

**Step 2**

Check if the returned array from step 1 contains any true value or not.

- If the returned array contains any true boolean value then the number will not be prime which means the number will be a composite number.
- If the returned array doesn't contain any single value then the number will be a prime number.
- To achieve this we will use:
`contains`

`!`

) operator to check if the returned array contains any true boolean value.`if..else`

block.

- Also, let's rename the function
`isDivisible`

`isPrime`

**Input:**

`xxxxxxxxxx`

`7`

**Code:**

`xxxxxxxxxx`

`%dw 2.0`

`output application/json`

`var isPrime = (num) -> `

` !contains((2 to num-1 map (((num mod $) == 0))), true)`

`---`

`if(isPrime(payload))`

` payload ++ " is a prime number."`

`else`

` payload ++ " is not a prime number. It is a composite number" `

**Output:**

`xxxxxxxxxx`

`"7 is a prime number."`

Let's check the output for payload as `6`

:

**Input:**

`xxxxxxxxxx`

`6`

** **

**Output:**

`xxxxxxxxxx`

`"6 is not a prime number. It is a composite number"`

**Step 3**

We need to handle the case of 0, 1, and 2. As the above code will not work for the inputs 0, 1, and 2. We will handle this case using **if..else **block.

**Input:**

`xxxxxxxxxx`

`0`

**Code:**

x

`%dw 2.0`

`output application/json`

`var isPrime = (num) -> `

` !contains((2 to num-1 map (((num mod $) == 0))), true)`

`---`

`if (payload == 0 or payload == 1)`

` "0 and 1 are neither prime nor composite"`

`else if (payload == 2)`

` "2 is a prime number."`

`else if (isPrime(payload) == true)`

` payload ++ " is a prime number"`

`else`

` payload ++ " is not a prime number. It is a composite number." `

**Output:**

`xxxxxxxxxx`

`"0 and 1 are neither prime nor composite"`

Let's check the output for payload as `2`

:

**Input:**

`xxxxxxxxxx`

`2`

**Output:**

`xxxxxxxxxx`

`"2 is a prime number."`

In this way, we have achieved the functionality of checking whether the input number is prime or not using DataWeave 2.0. But we can further optimize this code by the following way,

- In our existing code, we are running our loop from 2 to (number-1 ).
- But the number will be always indivisible by the divisors which are greater than (number/2).
- For example, assume the number is 7. Our existing code will check the number 7 for the divisibility by 2,3,4,5,6. But 7 will be always indivisible by the numbers which are greater than (7/2) = 3 i.e., 4,5,6.
- By considering this fact we can run our loop from 2 to (number/2) only.
- This will reduce the execution time of the code.

So the final codes are as follows:

**Code:**

`xxxxxxxxxx`

`%dw 2.0`

`output application/json`

`var isPrime = (num) -> `

` !contains((2 to num-1 map (((num mod $) == 0))), true)`

`---`

`if (payload == 0 or payload == 1)`

` "0 and 1 are neither prime nor composite"`

`else if (payload == 2)`

` "2 is a prime number."`

`else if (isPrime(payload) == true)`

` payload ++ " is a prime number"`

`else`

` payload ++ " is not a prime number. It is a composite number." `

**Optimized Code:**

`xxxxxxxxxx`

`%dw 2.0`

`output application/json`

`var isPrime = (num) -> `

` !contains((2 to num/2 map (((num mod $) == 0))), true)`

`---`

`if (payload == 0 or payload == 1)`

` "0 and 1 are neither prime nor composite"`

`else if (payload == 2)`

` "2 is a prime number."`

`else if (isPrime(payload) == true)`

` payload ++ " is a prime number"`

`else`

` payload ++ " is not a prime number. It is a composite number." `

I hope this article will help you to improve your logical and DataWeave skills. **Thanks!**

Opinions expressed by DZone contributors are their own.

Comments