Square root digital expansion

It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.

The square root of two is 1.41421356237309504880…, and the digital sum of the first one hundred decimal digits is 475.

For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.


平方根数字展开

众所周知,如果一个自然数的平方根不是整数,那么就一定是无理数。这样的平方根的小数部分是无限不循环的。

2的平方根为1.41421356237309504880…,它的小数点后一百位数字的和是475。

对于前一百个自然数,求所有无理数平方根小数点后一百位数字的总和。

解题

问题:如何求无理数的一百位小数?这真是无理取闹

参考博客

在上面给的博客中给了一个很好的方法

对于 数 n 我们需要去根号n,如下很有意思的规律

def Suqareroot(n,digits):
limit = 10**(digits+ 1)
a = 5*n
b = 5
while b < limit:
if a>= b:
a -= b
b +=10
else:
a *= 100
b = int(b/10) * 100 + 5
return int(b/100)

说明下:

1.题目让求的是小数点前100位的值,包括整数位

2.上面算法只有最后b/100 是根号n的近似解,这里是去小数点的,只有为什么不是b表示不理解

JAVA

package Level3;

import java.math.BigInteger;
import java.util.ArrayList; public class PE080{ void run(){
int j = 1;
int res = 0;
for(int i=1;i<=100;i++){
if(j*j==i){
j++;
continue;
}
res += Int_Sum(Squareroot(i,100));
}
System.out.println(res);
}
private Integer Int_Sum(BigInteger b){
int res = 0;
String str = b.toString();
for(int i=0;i<str.length() ;i++){
res += str.charAt(i) - '0';
}
return res;
}
private BigInteger Squareroot(int n,int digits){
// 定义上界
BigInteger limit = new BigInteger("10").pow(digits+1);
BigInteger five = new BigInteger("5");
BigInteger ten = new BigInteger("10");
BigInteger hunderd = new BigInteger("100");
BigInteger a = new BigInteger(n+"").multiply(five);
BigInteger b = five;
while( b.compareTo(limit) < 0){
if(a.compareTo(b) >=0){
a = a.subtract(b);
b = b.add(ten);
}else{
a = a.multiply(hunderd);
b = b.divide(ten).multiply(hunderd).add(five);
}
}
return b.divide(hunderd);
} public static void main(String[] args){
long t0 = System.currentTimeMillis();
new PE080().run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}

40886
running time=0s34ms

 

Python

import time ;

def Suqareroot(n,digits):
limit = 10**(digits+1)
a = 5*n
b = 5
while b < limit:
if a>= b:
a -= b
b +=10
else:
a *= 100
b = int(b/10) * 100 + 5
return int(b/100) def Int_Sum(n):
res = sum(map(lambda x:int(x),unicode(n)))
return res
if __name__=='__main__':
t0 = time.time()
limit = 1000000
result = 0
j = 1
for i in range(1,101):
if j*j == i:
j+=1
continue
result += Int_Sum(Suqareroot(i,100))
print result
t1 = time.time()
print "running time=",(t1-t0),"s" #
# running time= 0.039999961853 s

这样的 程序好无节操

from decimal import Decimal,getcontext
getcontext().prec=102
N = set(range(2,100)) - set([4,9,16,25,36,49,64,81])
s = 0
for n in N:
d = Decimal(n).sqrt()
s += sum([int(i) for i in str(d).replace(".","")[:100]])
print(s)

Project Euler 80:Square root digital expansion 平方根数字展开的更多相关文章

  1. Project Euler #80: Square root digital expansion

    from decimal import getcontext, Decimal def main(): n = int(raw_input()) p = int(raw_input()) getcon ...

  2. Project Euler 57: Square root convergents

    五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...

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

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

  4. Project Euler 92:Square digit chains C++

    A number chain is created by continuously adding the square of the digits in a number to form a new ...

  5. Project Euler 90:Cube digit pairs 立方体数字对

    Cube digit pairs Each of the six faces on a cube has a different digit (0 to 9) written on it; the s ...

  6. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

  7. Project Euler 59: XOR decryption

    计算机上的每个字母都对应一个独特的编号,普遍接受的标准是ASCII(美国信息交换标准代码).例如,大写字母的A的ASCII码是65,星号(*)的ASCII码是42,而小写字母k的代码是107. 一种现 ...

  8. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

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

    本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square dif ...

随机推荐

  1. Java包的命名规则

    按照惯例,包申明遵循特定的格式.虽然不是严格要求的Java语法,如果不遵循格式要求,大多数的Java认为你是不懂Java. 从右到左的顺序是: 1.systaxExample表明包的本地名称. 2.e ...

  2. linux的7种运行级别

    Linux有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多用户 ...

  3. java 语法糖

    package syntax.autoCase; import java.util.Arrays; import java.util.List; public class autoCase { pub ...

  4. CLR via C# 内存管理读书记

    1. CLR 垃圾回收采用基于代的机制, 在一次垃圾回收中存活下来的对象被提升到另一代 2. 在确认对象是否垃圾时,从一组根开始,根包括静态字段,方法参数,局部变量等 3. 使用CriticalFin ...

  5. 【Web学习日记】——在IIS上发布一个WebService

    没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...

  6. Mac OS X 10.10.2 Yosemite jdk 环境变量配置

    我的Mac系统版本是OS X 10.10.2 Yosemite,为了用Eclipse做android开发,安装了jdk 1.7, 但是如果想使用IntelliJ IDE做android开发的话,就需要 ...

  7. SQLserver关于教学楼中教室在某个时间段是否被占用的数据库设计

    需求:关于教学楼中教室在周几某个时间段是否被占用的数据库设计 具体代码如下: -------------------教学楼维度 CREATE TABLE TeacBuild ( tbID int no ...

  8. MVC初学 - The type or namespace name 'DbContext' could not be found

    问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...

  9. mutable和volatile关键字

    1.mutable 在C++中,mutable是为了突破const的限制而设置的.被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其 ...

  10. 使用go语言后的感受

    前两天我说过为了学习go语言去学习了一遍python,当我完成了python的学习后,昨天中午就去学习了go语言.以下简称之为golang. 我用的操作系统是windows xp,golang对xp还 ...