【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 ...
随机推荐
- vue 异步提交php 两种方式传值
1.首先要在php的入口文件写上一条代码,允许异步提交 header("ACCESS-CONTROL-ALLOW-ORIGIN:*"); 2.在vue有两种方式将数据异步提交到ph ...
- .net core中的那些常用的日志框架(NLog篇)
前言 咱们上回讲到,.net core中内置的Logging日志框架的使用,以及浅显的讲解,接下来,给大家介绍一个第三方日志框架(NLog). NLog简介 NLog是适用于各种.NET平台(包括.N ...
- vue 项目打包后静态资源加载不到
1, 2,
- 正式班D8
2020.10.15星期四 正式班D8 一.上节课复习 OSI七层协议 socket socket是对传输层以下的封装 IP+port标识唯一一个基于网络通讯的软件 TCP与UDP TCP:因为在通信 ...
- day41 Pyhton 并发编程04
内容回顾 socket 最底层的网络通信 所有的网络通信都是基于socket 进程 什么是进程? 是操作系统的发展过程中,为了提高cpu的利用率,在操作系统同时运行多个程序的时候,为了数据的安 ...
- day20 Pyhton学习 面向对象-成员
一.类的成员 class 类名: # 方法 def __init__(self, 参数1, 参数2....): # 属性变量 self.属性1 = 参数1 self.属性2 = 参数2 .... # ...
- intellij idea:设置java方法注释模板(intellij idea 2019.2)
一,打开方法注释模板的编辑窗口 菜单file->打开settings editor栏目下->打开Live Templates 说明:刘宏缔的架构森林是一个专注架构的博客,地址:http ...
- 4.QOpenGLWidget-对三角形进行纹理贴图、纹理叠加
在上章3.QOpenGLWidget-通过着色器来渲染渐变三角形,我们为每个顶点添加颜色来增加图形的细节,从而创建出有趣的图像.但是,如果想让图形看起来更真实,我们就必须有足够多的顶点,从而指定足够多 ...
- 国产化即时通信系统开发 -- 实现GGTalk的登录界面(Linux、Ubuntu、UOS、中标麒麟)
距离2013年开源GGTalk以来,7年已经过去了,GGTalk现在有了完整的PC版.安卓版.iOS版(即将发布),以及Xamarin版本. 然而,时代一直在变化,在今天,有个趋势越来越明显,那就是政 ...
- 从0实现python批量爬取p站插画
一.本文编写缘由 很久没有写过爬虫,已经忘得差不多了.以爬取p站图片为着手点,进行爬虫复习与实践. 欢迎学习Python的小伙伴可以加我扣群86七06七945,大家一起学习讨论 二.获取网页源码 爬取 ...