【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 ...
随机推荐
- 【数量技术宅|金融数据分析系列分享】为什么中证500(IC)是最适合长期做多的指数
更多精彩内容,欢迎关注公众号:数量技术宅.探讨数据分析.量化投资问题,请加技术宅微信:sljsz01 投资股票指数相比个股的优势 我们在投资股票的时候,如果持仓集中在一只或者有限几只股票上,恰好不幸遇 ...
- C# excel文件导入导出
欢迎关注微信公众号 C#编程大全 这里有更多入门级实例帮你快速成长 在C#交流群里,看到很多小伙伴在excel数据导入导出到C#界面上存在疑惑,所以今天专门做了这个主题,希望大家有所收获! 环境:wi ...
- ansible-playbook流程控制-when条件判断
1. ansible-playbook添加判断 when相当于shell脚本里的if 判断,when语句就是用来实现这个功能的,它是一个jinja2的语法,但是不需要双大括号,用法很简单 1 ...
- Python+Appium自动化测试(14)-yaml配置Desired capabilities
一,前言 在之前的appium自动化测试示例中,我们都是把构造driver实例对象的数据(即Desired Capabilities)写在业务代码里,如下: # -*- coding:utf-8 -* ...
- Avoid mutating a prop directly since the value will be overwritten whenever the parent component re
子组件修改父组件的值踩坑 Vue1.0升级至2.0之后,直接在子组件修改父组件的值是会报错的 目的是为了阻止子组件影响父组件的数据. 我们都知道在vue中,父组件传入子组件的变量是存放在props属性 ...
- js 为什么0.1+0.2不等于0.3
当程序员在使用浮点数进行计算逻辑处理时,不注意,就可能出现问题, 记住,永远不要直接比较俩个浮点的大小 这个属于数字运算中的精度缺失的问题 在0.1 + 0.2这个式子中,0.1和0.2都是近似表示的 ...
- 【贪心算法】HDU 5969 最大的位或
题目内容 Vjudge链接 给出一个闭区间,找该区间内两个数,使这两个数的按位或最大. 输入格式 包含至多\(10001\)组测试数据. 第一行有一个正整数,表示数据的组数. 接下来每一行表示一组数据 ...
- 【CF1428D】Bouncing Boomerangs 题解
原题链接 题意简介 毒瘤大模拟 给你一张n*n的图,在图上摆有一些物体.从每一列的底端往上扔回旋镖,每镖中一个东西,回旋镖就会向右转九十度.现在我们知道从每列i底端往上镖时撞上的物体个数ai,试构造出 ...
- 如何使用 Gin 和 Gorm 搭建一个简单的 API 服务 (二)
创建 API 我们之前已经跑过 Gin 框架的代码,现在是时候加些功能进去了. 读取全部信息 我们先从"增删改查"中的"查"入手,查询我们之前添加的信息.我接下来要删除几行代码,并把 Gin ...
- BOOST 条件变量使用
代码: // boost库 条件变量 使用测试 #include <iostream> #include <boost/thread.hpp> using namespace ...