# 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.

· Integration Zone · Code Snippet
Save
10.69K Views

## 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` function from `dw::Core` module of DataWeave.
• `mod `operator to check if the number is completely divisible by the divisor or not.

Input:

JSON

`xxxxxxxxxx`
1

1
`7`

Code:

Java

`xxxxxxxxxx`
1

1
`%dw 2.0`
2
`output application/json`
3
`var isDivisible = (num) ->   `
4
`    (2 to num-1 map (((num mod \$) == 0)))`
5
`---`
6
`isDivisible(7)`

Output:

JSON

`xxxxxxxxxx`
1

1
`[`
2
`  false,`
3
`  false,`
4
`  false,`
5
`  false,`
6
`  false`
7
`]`

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

Input:

JSON

`xxxxxxxxxx`
1

1
`6`

Output:

JSON

`xxxxxxxxxx`
1

1
`[`
2
`  true,`
3
`  true,`
4
`  false,`
5
`  false`
6
`]`

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

1. payload = `7`
• We wrote lambda function `isDivisible` to return the array of boolean values. Each of these boolean values from the array represents the divisibility of the number by the divisor (loop element).
• 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]`.
2. 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]`.

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` function from DataWeave along with not (`!`) operator to check if the returned array contains any true boolean value.
• `if..else` block.
• Also, let's rename the function `isDivisible` to `isPrime` as now we are heading towards checking if the number is prime or not.

Input:

JSON

`xxxxxxxxxx`
1

1
`7`

Code:

Java

`xxxxxxxxxx`
1

1
`%dw 2.0`
2
`output application/json`
3
`var isPrime = (num) ->   `
4
`    !contains((2 to num-1 map (((num mod \$) == 0))), true)`
5
`---`
6
`if(isPrime(payload))`
7
` payload ++ " is a prime number."`
8
`else`
9
` payload ++ " is not a prime number. It is a composite number" `

Output:

JSON

`xxxxxxxxxx`
1

1
`"7 is a prime number."`

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

Input:

JSON

`xxxxxxxxxx`
1

1
`6`

Output:

JSON

`xxxxxxxxxx`
1

1
`"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:

JSON

`xxxxxxxxxx`
1

1
`0`

Code:

Java

`x`

1
`%dw 2.0`
2
`output application/json`
3
`var isPrime = (num) ->  `
4
`    !contains((2 to num-1 map (((num mod \$) == 0))), true)`
5
`---`
6
`if (payload == 0 or payload == 1)`
7
`    "0 and 1 are neither prime nor composite"`
8
`else if (payload == 2)`
9
`    "2 is a prime number."`
10
`else if (isPrime(payload) == true)`
11
`    payload ++ " is a prime number"`
12
`else`
13
`    payload ++ " is not a prime number. It is a composite number."    `

Output:

JSON

`xxxxxxxxxx`
1

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

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

Input:

JSON

`xxxxxxxxxx`
1

1
`2`

Output:

JSON

`xxxxxxxxxx`
1

1
`"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:

Java

`xxxxxxxxxx`
1
13

1
`%dw 2.0`
2
`output application/json`
3
`var isPrime = (num) ->  `
4
`    !contains((2 to num-1 map (((num mod \$) == 0))), true)`
5
`---`
6
`if (payload == 0 or payload == 1)`
7
`    "0 and 1 are neither prime nor composite"`
8
`else if (payload == 2)`
9
`    "2 is a prime number."`
10
`else if (isPrime(payload) == true)`
11
`    payload ++ " is a prime number"`
12
`else`
13
`    payload ++ " is not a prime number. It is a composite number."    `

Optimized Code:

Java

`xxxxxxxxxx`
1
13

1
`%dw 2.0`
2
`output application/json`
3
`var isPrime = (num) ->  `
4
`    !contains((2 to num/2 map (((num mod \$) == 0))), true)`
5
`---`
6
`if (payload == 0 or payload == 1)`
7
`    "0 and 1 are neither prime nor composite"`
8
`else if (payload == 2)`
9
`    "2 is a prime number."`
10
`else if (isPrime(payload) == true)`
11
`    payload ++ " is a prime number"`
12
`else`
13
`    payload ++ " is not a prime number. It is a composite number."    `