Largest exponential

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 over three million digits.

Using base_exp.txt(right click and ‘Save Link/Target As…’), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.

NOTE: The first two lines in the file represent the numbers in the example given above.


最大的幂

比较两个如211和37这样写成幂的形式的数并不困难,任何计算器都能验证211 = 2048 < 37 = 2187。

然而,想要验证632382518061 > 519432525806就会变得非常困难,因为这两个数都包含有超过三百万位数字。

22K的文本文件base_exp.txt(右击并选择“目标另存为……”)有一千行,每一行有一对底数和指数,找出哪一行给出的幂的值最大。

注意:文件的前两行就是上述两个例子。

解题

指数运算太大了,取对数不就可以了

百度百科找的图片。

原函数和其反函数关于y=x对称,并且单调性一样。

JAVA

package Level3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet; public class PE099{
public static void run(){
ArrayList<ArrayList<Integer>> base_exp = base_exp();
int size = base_exp.size();
double result[] = new double[size];
double MAX = 1.0*Integer.MIN_VALUE;
int index = 0;
for(int i =0;i<size;i++){
ArrayList<Integer> bp = base_exp.get(i);
int base = bp.get(0);
int exp = bp.get(1);
result[i] = log10(base,exp);
// System.out.println(result[i]);
if(MAX < result[i]){
MAX = result[i];
index = i;
}
}
// 要加一,你懂的
index+=1;
System.out.println(index);
}
// 709
// running time=0s22ms
public static double log10(int base,int exp){
double res = 0.0;
res = exp*Math.log10(base);
return res;
}
public static ArrayList<ArrayList<Integer>> base_exp(){
String filename = "src/Level3/p099_base_exp.txt";
ArrayList<ArrayList<Integer>> base_exp = new ArrayList<ArrayList<Integer>>(); try {
BufferedReader input = new BufferedReader(new FileReader(filename));
String str="";
try {
while((str=input.readLine())!=null){
String[] strArr = str.split(",");
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(Integer.parseInt(strArr[0]));
num.add(Integer.parseInt(strArr[1]));
base_exp.add(num);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return base_exp; }
public static void main(String[] args) throws IOException{
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}

Python

# coding=gbk

import time as time
import re
import math
def run():
filename = 'E:/java/projecteuler/src/Level3/p099_base_exp.txt'
file = open(filename)
MAX = 0.0
index = 0
i = 0
for row in file.readlines():
row = row.strip('\n').split(",")
res = int(row[1])*math.log(int(row[0]))
i+=1
if res>MAX:
MAX = res
index = i
print index
#
# running time= 0.00400018692017 s
t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"

Project Euler 99:Largest exponential 最大的幂的更多相关文章

  1. Project Euler:99 Largest exponential C++

    Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator woul ...

  2. Project Euler 11 Largest product in a grid

    题意:在这个20×20方阵中,四个在同一方向(从下至上.从上至下.从右至左.从左至右或者对角线)上相邻的数的乘积最大是多少? 思路:暴力去枚举以 ( x , y ) 为中心拓展的四个方向 /***** ...

  3. Project Euler 8 Largest product in a series

    题意:寻找这 1000 个数中相邻 13 个数相乘积最大的值 思路:首先想到的是暴力,但是还可以利用之前记录过的数值,什么意思呢?即在计算 2 - 14 后,再计算 3 - 15 时完全可以利用之前计 ...

  4. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  5. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  6. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  7. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  8. Python练习题 036:Project Euler 008:1000位数字中相邻13个数字最大的乘积

    本题来自 Project Euler 第8题:https://projecteuler.net/problem=8 # Project Euler: Problem 8: Largest produc ...

  9. Python练习题 031:Project Euler 003:最大质因数

    本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...

随机推荐

  1. jQuery滚动监听插件Waypoints

    页面内滚动操作的导航插件Waypoints.它可以让你方便的处理页面滚动事件,你可以比较自由的在自己的UI中使用这个插件控制页面滚动事件. Waypoints根据用户滚动的位置来帮助开发者构建相关的设 ...

  2. 如何在CentOS5中增加CentALT的源

    1. 建立centalt.repo    指令: vi /etc/yum.repos.d/centalt.repo 2. 將下面的內容貼進去    [CentALT]    name=CentALT ...

  3. Excel多条件筛选、公式填充

    接到一个任务,由于数据操作人员不会使用编辑公式进而无法进行相关筛选,所以要我帮忙.好久不碰Excel了,那就试试看吧. 需求是这样子的(这里做了最大化的简化):要求判断条件,男50岁以上,女40岁以上 ...

  4. SQL Server Profiler监控执行语句

    SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...

  5. winform:无法引用其他类库,dll,using等个人看法【图】

    在项目类库中已经引用了相关了类库,生成解决方案也没问题,但是到了后置代码,通过using引用其他类库的时候,再生成解决方案或者生成单个类库,就会报“未能找到类型或命名空间“xxx"(是否缺少 ...

  6. 【PHP】phpcms html去除空白

    // 文件路径:/phpcms/libs/classes/template_cache.class.php 42行 // 第四第五行是新增的 $content = $this->template ...

  7. Oracle 动态视图5 V$SESSION_LONGOPS

    一.视图V$SESSION_LONGOPS显示运行超过6秒的操作的状态.包括备份,恢复,统计信息收集,查询等等 Column Datatype Description SID NUMBER Sessi ...

  8. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  9. [转]- Winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    转自:http://heisetoufa.iteye.com/blog/382684 第一种方法: 用委托,Form2和Form3是同一组 Form2  using System; using Sys ...

  10. 第一章 Web MVC简介

    Web MVC简介 1.1.Web开发中的请求-响应模型: 在Web世界里,具体步骤如下: 1.  Web浏览器(如IE)发起请求,如访问hao123主页 2.  Web服务器(如Tomcat)接收请 ...