Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find prime numbers in arrays by JavaScript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge about "how JavaScript finds prime numbers in arrays". In the actual operation process of cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

What are prime numbers?

Prime numbers, also known as prime numbers, refer to natural numbers greater than 1 that have no other factors except 1 and itself.

JavaScript How to find prime numbers in an array

Use the for loop statement or filter() method to loop through the array, in each loop to determine whether an array element is prime, if yes, then output the element.

The method of determining whether it is a prime number is to divide a number by 2 to sqrt(this number). If it can be divided exactly, it indicates that the number is not a prime number, and vice versa.

Here's how to do it:

Using the for loop

var a = [31,33,35,37,39,41,43,45,57,49,51,53];for(var i=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report