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

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

Share

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

This article mainly explains "how to find primes". The content of the explanation is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn "how to find primes".

Preface

Today's interviewers are a nightmare for countless developers, but not many of them can hang interviewers, because most interviewers really have that skill. But in the interview, we small survivors can not completely deny that we can only make a single breakthrough-to brighten the eyes of the interviewer on a certain question. Well, let's share it today.

These days, the algorithm post volume does not say, the development post is also a bit inside the volume, the requirements for developers are getting higher and higher, and the interviewer is also deliberately "difficult" interviewers, usually like to go from shallow to deep, usually like to ask: do you know why? Do you know how it works? Something like that. And, in the past, only interviewers in large companies like to ask about algorithms, and employees in large factories have a good foundation, and many even have ACM experience or system exercise experience, which is easy to understand, but now some interviewers in small companies are also talking about xx algorithms and xx data structures.

Find a prime number

In such a process, I was not surprised when the interviewer asked me an algorithm question. I realized the top ten sorting principles, complexity analysis, and code handwriting, and I was familiar with all kinds of operations of linked lists and trees. but suddenly the surprised interviewer came to ask a prime question, and I restored the scene:

Interviewer: do you know how to find prime numbers?

Me: looking for primes?

Interviewer: yes, it is to find prime numbers.

Me: it's very simple to judge that a number is a prime, then there must be no two numbers (except themselves and 1) multiplied by it. You just need to enumerate to see if there are any numbers that can be divisible by it. If there is, it is not a prime. If not, then it is a prime.

The interviewer looked disappointed and said I was right, but I didn't get to the point. Let me be specific.

Let's start my show:

First of all, the stupidest way to determine whether n is a prime is to determine whether there is any enumeration [2Magne1] that can be divisible by n directly. If so, the return false is not a prime, otherwise it is a prime. The code is as follows:

Boolean isprime (int value) {for (int item2witi)

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