【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 ...
随机推荐
- python数据结构之二叉树的遍历实例
遍历方案 从二叉树的递归定义可知,一棵非空的二叉树由根结点及左.右子树这三个基本部分组成.因此,在任一给定结点上,可以按某种次序执行三个操作: 1).访问结点本身(N) 2).遍历该结点的 ...
- 使用HTML的基本结构创建网页
1. 网页的扩展名--html或htm 2. 如何新建网页? 步骤1: 在电脑的空白处,右键选择-->新建--文本文档 步骤2: 把txt的扩展名,改成html或htm, ...
- CyclicBarrier原来是这样的
上一篇聊了一下Semaphore信号灯的用法及源码,这一篇来聊一下CyclicBarrier的用法及解析. 官网解释: 允许一组线程全部等待彼此达到共同屏障点的同步辅助.循环阻塞在涉及固定大小的线程方 ...
- 动画演示Sunday字符串匹配算法——比KMP算法快七倍!极易理解!
前言 上一篇我用动画的方式向大家详细说明了KMP算法(没看过的同学可以回去看看). 这次我依旧采用动画的方式向大家介绍另一个你用一次就会爱上的字符串匹配算法:Sunday算法,希望能收获你的点赞关注收 ...
- MeteoInfo家族的新产品:MeteoInfoLab
为了更方便地处理各种数据并绘图,尝试开发了一个新的软件产品MeteoInfoLab,软件设计上参考了MatLab和Spider.软件以脚本程序和命令行交互为主,基于MeteoInfo库并利用Jytho ...
- Jenkins集成appium自动化测试(Windows篇)
一,引入问题 自动化测试脚本绝大部分用于回归测试,这就需要制定执行策略,如每天.代码更新后.项目上线前定时执行,才能达到最好的效果,这时就需要进行Jenkins集成. 不像web UI自动化测试可以使 ...
- 多路查找树(2-3 树、2-3-4 树、B 树、B+ 树)
本文参考自<大话数据结构> 计算机中数据的存储 一般而言,我们都是在内存中处理数据,但假如我们要操作的数据集非常大,内存无法处理了,在这种情况下对数据的处理需要不断地从硬盘等存储设备中调入 ...
- linux安装jdk-centos7系统:
1 官网下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- git merge 与 git rebase的区别?
一,git merge 与 git rebase的区别 1,git merge 例如: master分支合并dev分支,git将两个分支dev和master上的所有commit , 按照提交时间的先后 ...
- Vue企业级优雅实战05-框架开发01-登录界面
预览本文的实现效果: # gitee git clone git@gitee.com:cloudyly/dscloudy-admin-single.git # github git clone git ...