【kata Daily 190905】What's a Perfect Power anyway?(完美幂)
原题:
A perfect power is a classification of positive integers:
In mathematics, a perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, n is a perfect power if there exist natural numbers m > 1, and k > 1 such that mk = n.
Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, NULL, None or your language's equivalent.
Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.
Examples
isPP(4) => [2,2]
isPP(9) => [3,2]
isPP(5) => None
-----------------------------------------------------------------------------------------------------------------------------------
题目大意:给定一个数n,判断这个数是否是完美幂,即:有一个数m的k次数等于n。
解题思路:
我自己的解题思路很粗暴,但是并不能过审,这里也说一下我的思路
def isPP(n):
# your code here
for i in range(n):
for j in range(n):
if i**j == n:
return [i, j]
return None
没错。。。很笨而且很好资源的办法,,,ヽ(ー_ー)ノ
看一下其他网友的办法:
def isPP(n):
#your code here
from math import sqrt
m=int(sqrt(n))
for i in range(2,m+1):
k=0
while i**k < n:
k+=1
if i**k==n:
return [i,k]
return None
解读:先对n进行开根号,得到最大的m值,然后根据逐步逼近的办法来确定k的值。很好理解,是个好办法。
看一下最多人推荐的:
from math import ceil, log, sqrt def isPP(n):
for b in xrange(2, int(sqrt(n)) + 1):
e = int(round(log(n, b)))
if b ** e == n:
return [b, e]
return None
困惑:e 的根据是什么不太懂,,,
知识点:
1、数的幂次方运算,**两个星号表示幂运算:2**3=8
2、数的对数运算,math.log(x[, base]):base默认为e
3、数的开根号,math.sqrt(n)
4、逐步逼近的算法,根据判断的结果取最后的值用于运算
【kata Daily 190905】What's a Perfect Power anyway?(完美幂)的更多相关文章
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part8:Perfect Forwarding(完美转发):解决方案
本文为第八部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【Kata Daily 191012】Find numbers which are divisible by given number
题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)
题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...
- 【Kata Daily 190923】Odder Than the Rest(找出奇数)
题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...
- 【Kata Daily 190920】Square(n) Sum(平方加总)
题目: Complete the square sum function so that it squares each number passed into it and then sums the ...
- 【Kata Daily 190919】Sort Out The Men From Boys(排序)
题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...
随机推荐
- 排序算法:归并排序(Merge Sort)
归并排序 归并排序采用了分治策略(divide-and-conquer),就是将原问题分解为一些规模较小的相似子问题,然后递归解决这些子问题,最后合并其结果作为原问题的解. 归并排序将排序数组A[1. ...
- OpenCV计算机视觉学习(2)——图像算术运算 & 掩膜mask操作(数值计算,图像融合,边界填充)
在OpenCV中我们经常会遇到一个名字:Mask(掩膜).很多函数都使用到它,那么这个Mask到底是什么呢,下面我们从图像基本运算开始,一步一步学习掩膜. 1,图像算术运算 图像的算术运算有很多种,比 ...
- English 介词
English 介词 Create Time : 2019-06-27 表示时间的介词称为时间介词.表示时间的介词有:at, on, in, before, after等. 一.at, on和in ① ...
- 多测师讲解python_003.2练习题
# 1.分别打印100以内的所有偶数和奇数并存入不同的列表当中# 2.请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]# 里面的重复元素不能用set# 3.将字符 ...
- pytest文档48-切换 base_url 测试环境(pytest-base-url)
前言 当我们自动化代码写完成之后,期望能在不同的环境测试,这时候应该把 base_url 单独拿出来,能通过配置文件和支持命令行参数执行. pytest-base-url 是 pytest 里面提供的 ...
- (在模仿中精进数据可视化03)OD数据的特殊可视化方式
本文完整代码已上传至我的Github仓库https://github.com/CNFeffery/FefferyViz 1 简介 OD数据是交通.城市规划以及GIS等领域常见的一类数据,特点是每一条数 ...
- hugo官方相关文档地址
+++ date="2020-10-17" title="hugo官方相关文档地址" tags=["hugo"] categories=[& ...
- centos8安装kafka(单机方式)
一,下载kafka 1,官网地址 http://kafka.apache.org/downloads.html 2,下载 [root@localhost source]# wget http://mi ...
- appium_android-常见的问题
po模型的原则: 用公共方法代表UI所提供的功能 方法应该返回其他的PageObject或者返回用于断言的数据 同样的行为不同的结果可以建模为不同的方法 不要在方法内加断言 字段意义 不要暴露页面内部 ...
- <bdi> 标签
bdi 指的是 bidi 隔离. <bdi> 标签允许您设置一段文本,使其脱离其父元素的文本方向设置. 在发布用户评论或其他您无法完全控制的内容时,该标签很有用. 实例 把用户名从周围的文 ...