题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆化搜索即可,利用之前搜索的值加速搜索,如果当前搜索值在之前已经处理过,那么直接利用当前搜索值 + 到当前数的步数即为该数的步数 /************************************************************************* > File Name…
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 above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that this seque…
Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. The code resemble : import math limit = 2000000 crosslimit = int(math.sqrt(limit)) #sieve = [False] * limit sieve =…
1.If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 100 (计算1000以内能被3或5整除的数之和) #include <iostream> using namespace std; i…
题目的大意很简单 做法的话. 我们就枚举1~10000的数的立方, 然后把这个立方中的有序重新排列,生成一个字符串s, 然后对于那些符合题目要求的肯定是生成同一个字符串的. 然后就可以用map来搞了 这里偷懒用了python import string dic = {} def getstr(n): sn = str(n) arr = [] for c in sn: arr.append(c) arr.sort() return ''.join(arr) for i in xrange(1, 1…
组合数,2n中选n个.向右走有n步,向下走有n步.共2n步.有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n). 或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i][j] = step[i-1][j]+step[i][j-1],递推即可. #include <iostream> using namespace std; typedef long long ll; ll C(ll n, ll m) { ll res = 1; for(ll i = 1; i &…
还是素数线性筛 MAXN = 2000000 prime = [0 for i in range(MAXN+1)] res = 0 for i in range(2,MAXN+1): if prime[i] == 0: res += i prime[0] += 1 prime[prime[0]] = i j = 1 while j <= prime[0] and prime[j]<=MAXN/i: prime[prime[j]*i] = 1 if i%prime[j] == 0: break…
我是俩循环暴力 看了看给的文档,英语并不好,有点懵,所以找了个中文的博客看了看:勾股数组学习小记.里面有两个学习链接和例题. import math def calc(): for i in range(1,1000): j = i+1 while j <= 1000: c = i*i+j*j c = int(math.sqrt(c)) if i+j+c > 1000: break if i < j < c and i+j+c == 1000 and i*i+j*j == c*c:…
素数线性筛 MAXN = 110100 prime = [0 for i in range(210000)] for i in range(2,MAXN): if prime[i] == 0: prime[0] += 1 prime[prime[0]] = i j = 1 while j <= prime[0] and prime[j]<=MAXN/i: prime[prime[j]*i] = 1 if i%prime[j] == 0: break j += 1 print prime[100…
对每个数字分解素因子,最后对每个素因子去其最大的指数,然后把不同素因子的最大指数次幂相乘,得到的就是最小公倍数 python不熟练,代码比较挫 mp = {} def process(n): i = 2 while i*i <= n: cnt = 0 if n%i == 0: while n%i == 0: cnt += 1 n /= i if i in mp: mp[i] = max(mp[i], cnt) else: mp[i] = cnt i += 1 if n > 1: if n in…
本题来自 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…
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divisible triangular number # The sequence of triangle numbers is generated by adding the natural numbers. # So the 7th triangle number would be 1 + 2 + 3…
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names scores Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into al…
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest product in a grid # In the 20×20 grid below, four numbers along a diagonal line have been marked in red. # The product of these numbers is 26 × 63 × 78 ×…
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出这道题的人) program 4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.…
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we list all the natural numbers below 10 # that are multiples of 3 or 5, we get 3, 5, 6 and 9. # The sum of these multiples is 23. # Find the sum of all…
本题来自 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…