Project Euler 80:Square root digital expansion 平方根数字展开

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"

# 40886
# 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)
时间: 2024-08-10 15:42:44

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

Project Euler #80: Square root digital expansion

1 from decimal import getcontext, Decimal 2 3 4 def main(): 5 n = int(raw_input()) 6 p = int(raw_input()) 7 8 getcontext().prec = p+10 # 扩大精度,保证接过 9 sum = 0 10 11 for i in range(1,n+1): 12 nTemp = Decimal(i).sqrt() 13 if nTemp._isinteger() : # 自生函数的判

UVA 10023 - Square root(手算平方根)

题目:UVA 10023 - Square root(手算平方根) 题目链接 题目大意:求给定的一个数的平方根. 解题思路:用二分但是这个数太大了,就超时了.看题接后发现需要用一种手算平方根的算法. 算法: 先判断这个数是不是偶数位,是的话就第一次取前面的两位数,不是的话第一次就只取前面的一位数来作为被除数.接下来就是两位两位为一节来计算. 用前一次的计算结果乘上20+一个个位数a再乘上这个a,找到最大的a使得这个式子的结果不大于被除数. 被除数减去这个结果然后再在尾巴接上那个大数的接下来两位作

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 number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

Project Euler 80:Path sum: two ways 路径和:两个方向

Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.           131 673 234 103 18 201 96 342 965 150 630 803 74

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:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

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(