Project Euler 99: Largest exponential

比较像\(2^{11}\)和\(3^7\)这样用指数形式表示的数并不困难,因为任何计算器都可以确认:
\[
2^{11}=2048<3^7=2187
\]
然而,确认\(632382^{518601}>519432^{525806}\)就要困难得多,因为两个数都包含三百万位数。文本文件 base_exp.txt中包含一千行数据,每一行都是幂的底与指数的组合,求那一行的数值最大。

注:文本文件中的前两行即为上面例子中所列的两个数。

分析:此题的求解思路比较直接,虽然我们知道python默认支持大整数,可以计算出文件中所列的那些数字,但是直接这样计算效率上会有损失。考虑到题目只是要比较这些指数的大小,而不是要求计算出这些指数,所以我们可以利用对数的性质把上述指数转化为较小的数字来进行比较。我们都知道对数函数是一个单调递增函数,所以如果\(log(x)>log(y)\),则我们有\(x>y\),同时我们知道\(log(a^b)=blog(a)\),则我们可以对文件中每一行的指数直接取对数,然后比较这些对数的大小,最大的对数就对应最大的指数,其对应的行数即是题目所求。

# time cost = 1.51 ms ± 24.9 μs

from math import log10

def main():
    with open('data/ep99.txt') as f:
        pairs = [list(map(int,s.split(','))) for s in f.readlines()]
    log_pairs = [x[1]*log10(x[0]) for x in pairs]
    return log_pairs.index(max(log_pairs))+1

原文地址:https://www.cnblogs.com/metaquant/p/12198151.html

时间: 2024-08-30 04:39:29

Project Euler 99: Largest exponential的相关文章

Project Euler:99 Largest exponential C++

Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator would confirm that 211 = 2048 < 37 = 2187. However, confirming that 632382518061 > 519432525806 would be much more difficult, as both numbers contain ove

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b

Python练习题 047:Project Euler 020:阶乘结果各数字之和

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

Python练习题 046:Project Euler 019:每月1日是星期天

本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Answer: 171 ''' from datetime import * firstDay = date(1901,1,1) lastDay = date(

Python练习题 035:Project Euler 007:第10001个素数

本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime # By listing the first six prime numbers: # 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? # Answer

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Project Euler 126 - Cuboid layers

这题先是推公式- 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 f(t,x,y,z)=4(t?1)(x+y+z+t?2)+2(xy+yz+xz) 表示长宽高为x .y .z 的立方体第t 层放的立方体的个数. 接下来就是算答案了- 方法很简单:暴力 但是暴力还是有技巧的,开始我是直接从1到1000枚举t .x .y .z ,但这样出不来结果. 换成下面代码里的方法就行了. 1 #include <iostream> 2 #include <cstdio> 3 #includ

Project Euler 第一题效率分析

Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平台打造一个有趣和休闲 的环境. 项目主页:https://projecteuler.net 第一题 Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we ge

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule