当前位置:网站首页>The third divisor of leetcode simple question
The third divisor of leetcode simple question
2022-04-23 08:14:00 【·Starry Sea】
subject
Give you an integer n . If n There are exactly three positive divisors , return true ; otherwise , return false .
If there are integers k , Satisfy n = k * m , So integers m Namely n One of the Divisor .
Example 1:
Input :n = 2
Output :false
explain :2 There are only two divisors :1 and 2 .
Example 2:
Input :n = 4
Output :true
explain :4 There are three divisors :1、2 and 4 .
Tips :
1 <= n <= 10^4
source : Power button (LeetCode)
Their thinking
First of all, a number must have more than two divisors, which is 1 And itself , We can set up a flag when 2 and n-1 Found a divisor between , So conversion tag , If the divisor is encountered again, the check mark can return the result , In addition, if you don't encounter divisor , It shows that this number is a prime number and can also return the result .
class Solution:
def isThree(self, n: int) -> bool:
if n==2:
return False
flag=False
for i in range(2,n):
if n%i==0:
if flag:
return False
flag=True
return flag

Actually, a number with three divisors , One is 1 The other is itself , There is also a pair of divisors that must be equal . But finding this logarithm must start from 2 Start looking for , This is not biased .
class Solution:
def isThree(self, n: int) -> bool:
for i in range(2,int(n**0.5)+1):
if n%i==0:
return i**2==n
return False

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590377.html
边栏推荐
猜你喜欢

NLLLoss+log_SoftMax=CE_Loss

How to import Excel data in SQL server, 2019 Edition

LeetCode简单题之计算字符串的数字和

vivo,硬件安全的爱与雷霆

Canvas learning Chapter 1

岛屿的个数

Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结

每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9

LeetCode中等题之旋转函数

Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
随机推荐
有意思的js 代码
LeetCode15. 三数之和
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
Flatten arrays
如何在SQL Server中导入excel数据,2019版
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
几种智能机器人室内定位方法对比
学fpga(从verilog到hls)
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
Move layout (Flex layout, viewport label)
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
Solidity IDE Remix中文版使用手册
AAAI 2022招募讲者啦!!
BUFFCTF文件中的秘密1
单点登录 SSO
Codeforces Round #784 (Div. 4)
Feign source code analysis
Convert object to URL
mysql查询字符串类型的字段使用数字类型查询时问题