当前位置:网站首页>Baidu classic interview question - determine prime (how to optimize?)
Baidu classic interview question - determine prime (how to optimize?)
2022-07-24 20:43:00 【Chen Yikang】
Catalog
Option two ( Primary optimization )
Option three ( Advanced optimization )
Scheme 1 ( Not optimized )
Most people may think of the following methods
public class Main(){
public static void main11(String[] args){
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
int i = 0;
for(i = 2; i < input; i++){
if(input % i == 0){
break;
}
}
if(i == input){
System.out.println(input + " Prime number !");
}
else{
System.out.println(input + " Not primes !");
}
}
}analysis :
To divide 2 ~ (n - 1) A digital , Efficiency is too low , The interviewer must not be satisfied (doge)
Option two ( Primary optimization )
Those with a little experience may think of this
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
int i = 0;
for(i = 2; i <= input/2; i++){
if(input % i == 0){
break;
}
}
if(i > input/2){
System.out.println(input + " Prime number !");
}
else{
System.out.println(input + " Not primes !");
}
}analysis :
for example 16 The number of , It can be written. 1*16、2*8、4*4, Carefully observe that at least one number in each group is less than or equal to n/2, So just remove 2 ~ n/2 The number between , It reduces the amount of calculation by nearly half , At this time , The interviewer may also say : Nice guy , Can you optimize it again ?
Option three ( Advanced optimization )
Some experienced people may think of
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
int i = 0;
for (i = 2; i <= Math.sqrt(input); i++) {
if (input % i == 0) {
break;
}
}
if (i > Math.sqrt(input)) {
System.out.println(input + " Prime number !");
} else {
System.out.println(input + " Not primes !");
}
}analysis :
for example 16 It can be written. 1*16、2*8、4*4, Observe carefully , At least one number in each group of data is less than or equal to the root 16, So the amount of calculation can be reduced to 2~ Radical sign 16, The interviewer may say : good ~ Young man , Join our company !
边栏推荐
- [training Day8] tent [mathematics] [DP]
- whistle ERR_ CERT_ AUTHORITY_ INVALID
- Top 10 in China's HCI software market: Huawei, Xinhua, Shenxin, VMware, Lenovo, smartx, Inspur, Qingyun, lutanli, dawn
- [training Day10] point [enumeration] [bidirectional linked list]
- 93. Recursive implementation of combinatorial enumeration
- How to choose securities companies that support flush? Is it safe to open an account on your mobile phone
- 147 set whether to cache by using the routing meta information - use of include and exclude - use of activated and deactivated
- 2787: calculate 24
- The difference between token and session, this classic interview question deserves a more in-depth answer
- [FreeRTOS] 10 event flag group
猜你喜欢

API data interface for historical data of A-share index

Sword finger offer 15. number of 1 in binary

Chrome realizes automated testing: recording and playback web page actions
![[training Day9] light tank [dynamic planning]](/img/69/e7a69972a2865408479c7f8c245c1f.png)
[training Day9] light tank [dynamic planning]

驱动子系统开发
![[training Day8] interesting number [digital DP]](/img/39/caad2ccff916d5ab0f8c3d93f3901d.png)
[training Day8] interesting number [digital DP]

The difference between token and session, this classic interview question deserves a more in-depth answer
![[training Day9] rotate [violence] [thinking]](/img/b9/598dd0dffb9c82230f43484f9c8a1e.png)
[training Day9] rotate [violence] [thinking]
Hilditch refinement (implementation I)

What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews
随机推荐
How to choose securities companies that support flush? Is it safe to open an account on your mobile phone
API data interface of A-share transaction data
PC port occupation release
vlan技术
Framework API online viewing source code
Kubernetes resource list: how to create resources?
Docker builds redis and clusters
How does starknet change the L2 landscape?
Transport layer protocol parsing -- UDP and TCP
[feature selection] several methods of feature selection
Pressing Ctrl will cause several key press messages
[training Day9] rotate [violence] [thinking]
[training Day6] triangle [mathematics] [violence]
Upgrade appium automation framework to the latest 2.0
What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews
[mathematical modeling / mathematical programming model]
[training Day10] silly [simulation] [greed]
Alibaba sentinel basic operation
[advanced data processing technology] data filtering, advanced data filling, initial and advanced data transformation
ma.glasnost.orika. MappingException:No converter registered for conversion from Date to LocalDateTime