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
Newsletter
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

  • Passwordless Image Pulls in Kubernetes With ServiceAccount Tokens and a Credential Provider
  • Your Infrastructure Didn't Change, Your Policy Did: Detecting Governance-Transition Drift in IaC
  • Using LLMs to Generate Unit Tests and Review Pull Requests in Spring Boot
  • Don't Adopt Anything Announced at re:Invent for 90 Days

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 21, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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