DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering React: Common Interview Questions and Answers
  • Probing Questions for Your Interview
  • Interviews from the ALM Forum: Like SETI@home, but For Your Builds
  • TypeScript Interview Questions and Answers

Trending

  • Engineering Closed-Loop Graph-RAG Systems, Part 2: From Prompts to Rules
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  • Identity in Action
  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. DataWeave Interview Question: Find If Prime Number

DataWeave Interview Question: Find If Prime Number

This article will help you practice your DataWeave skills in MuleSoft. We're going to find out if an input number is prime or not.

By 
Gaurav Dubey user avatar
Gaurav Dubey
·
Feb. 21, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

A prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than themselves or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.

Note: Please use 3 or greater than 3 for this logic.

Input Payload:

JSON
 




xxxxxxxxxx
1


 
1
7


Output Result:

JSON
 




xxxxxxxxxx
1


 
1
"7 is prime number"


In Step 1, we will apply a map starting from 2 till (payload - 1).

After that, we will use the mod operator to check reminders of payload by item.

Step 1 Code:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
(2 to payload-1) map ((item, index) -> payload mod item)



Step 1 Output:

JSON
 




xxxxxxxxxx
1


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



In Step 2, we will use an if statement and check whether the output array of our previous step contains 0 or not.

If 0 is present it means our payload is completely divided by another number and hence it is not a prime number.

If 0 is not present it means our payload is not completely divided by another number and hence it is a prime number.

Final Code:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
if(((2 to payload-1) map ((item, index) -> payload mod item) contains 0) == true) payload ++ " is not prime number" else payload ++ " is prime number"


Output:

JSON
 




xxxxxxxxxx
1


 
1
"7 is prime number"



Hope this helps improve your DataWeave skills. I am not an expert on DataWeave. I am just sharing what I can solve. Comments are welcome. Thanks.

PRIME (PLC) Interview (journalism)

Opinions expressed by DZone contributors are their own.

Related

  • Mastering React: Common Interview Questions and Answers
  • Probing Questions for Your Interview
  • Interviews from the ALM Forum: Like SETI@home, but For Your Builds
  • TypeScript Interview Questions and Answers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook