【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 ...
随机推荐
- 055 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 02 数组的概念
055 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 02 数组的概念 本文知识点:数组的概念 数组的声明创建.初始化 在学习数组的声明创建.初始化前,我们可以和之 ...
- Python中matplotlib.pyplot.imshow画灰度图的多种方法
转载:https://www.jianshu.com/p/8f96318a153f matplotlib库的教程和使用方法此处就不累赘了,网上有十分多优秀的教程资源.此处直接上代码: def demo ...
- 用集装箱装ASP。带有Docker和Azure Kubernetes服务的NET Core应用程序
介绍 曾经有一个单一软件应用程序的时代,整个应用程序被打包并部署在作为单个进程运行的单个服务器上.我们都知道,在这个模型中,单点故障可能会导致整个应用程序崩溃. 微服务体系结构的发展是为了解决单片应用 ...
- NOIP提高组2018 D1T3 【赛道修建】
颓了好几天,终于把这到题处理了一下. 话说,其实我考场上想出正解了,但是手残,算复杂度的时候多按了一个零,导致算出来是1亿多的复杂度,都不敢打...就把部分分都捡了一下... 题目描述: C 城将要举 ...
- Springboot集成logback,控制台日志打印两次,并且是不同的线程打印的
背景 在搭建一个新项目的时候,从公司别的项目搞了个logback-spring.xml的配置过来,修改一下启动项目的时候发现 所有的日志都输出了两次 并且来自于不同的线程,猜测是配置重复了,但是仔细检 ...
- Springcloud技术分享
Springcloud技术分享 Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来 ...
- bash 在指定目录查找包含特定关键字的文件
比如我们要在目录/usr/local/nginx/conf/vhost/下查找baidu.com这个关键字的文件 方法1: find /usr/local/nginx/conf/vhost/ -exe ...
- 2014年 实验五 Internet与网络工具的使用
实验五 Internet与网络工具的使用 [实验目的] ⑴.FTP服务器的架设和客户端的使用. ⑵.使用云盘和云笔记应用 ⑶.运用QQ的远程协助功能. (4).默认安装foxmail软件,进行邮件 ...
- Lock、Synchronized锁区别解析
上篇博文在讲解 ConcurrentHashMap 时说到 1.7 中 put 方法实现同步的方式是使用继承了 ReentrantLock 类的 segment 内部类调用 lock 方法实现的,而在 ...
- 论减少代码中return语句的骚操作
一.写作背景 最近组内在推行checkstyle代码规范的检测,关于checkstyle的介绍可以参考:https://checkstyle.sourceforge.io, 在按照checkstyle ...