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() : # 自生函数的判断
14             sum += 0
15         else:
16             nTemp = str(nTemp)
17             for _ in range(p+1): # 空下来一个.,所以+1
18                 if nTemp[_] != ‘.‘:
19                     sum += int(nTemp[_])
20
21     print sum
22
23 main()

学习

  抽象化理解题目

  边界值问题

错误

  读题不仔细

Link:

  https://www.hackerrank.com/contests/projecteuler/challenges/euler080

时间: 2024-10-12 04:07:57

Project Euler #80: Square root digital expansion的相关文章

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 i

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'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(

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