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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • The Perfection Trap: Rethinking Parkinson's Law for Modern Engineering Teams
  • Advancing Robot Vision and Control
  • A Guide to Auto-Tagging and Lineage Tracking With OpenMetadata
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  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
9.9K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!