Longest Collatz sequence
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 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 sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
1.函数递归调用12345678910111213141516171819202122232425262728293031/*思路:简单递归1.if x == 1, return 1;2.if x == odd, return (3n + 1);3.if x == even, return (n / 2);*/#include<iostream>using namespace std;int func(int x){ if(x == 1){ return 1; } if(x % 2 == 0){ return func(x / 2) + 1; } return func(3 * x + 1) + 1; }int main() ...
Largest product in a series
Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166370484403199890008895243450658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408071984038509624554443629812309878799272442849091888458015616 ...
Largest product in a grid
Largest product in a grid
In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 6552 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 9122 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 8024 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 5032 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 7067 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 2124 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 7221 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 9578 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 9216 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 5786 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 5819 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 4004 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 6688 3 ...
Largest palindrome product
Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
1.枚举
利用两个for循环枚举两个三位数相乘
判断相乘的乘积是否是回文整数(将数字倒置判断是否等于原数字
动态刷新最大回文整数
12345678910111213141516171819202122232425262728293031#include <iostream>using namespace std;//自定义判断回文数字func函数int func(int num){ int raw = num, reverse = 0; while(num){ //(1)将x值的最后一位放入reverse中,并且向前移动一位 reverse = reverse * 10 + num % 10; //(2)去掉x值的最后一位 num /= 10; } return raw == reverse;}int main(){ int ans = 0; //1.利用两个for循环枚举两个三位数相乘 for(int i = 0; i < 1000; i++){ for(int j = i; j < 1000; j++){ //2.判断相乘的乘积是否为回文数字 if(func(i * j)){ //3.动态刷新最大回文数字 ans = max(ans, i * j); } } } cout << ans ...
Multiples of 3 and 5
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 the multiples of 3 or 5 below 1000.
1.枚举123456789101112#include <iostream>using namespace std;int main(){ int ans = 0; //时间复杂度:O(n) for(int i = 1; i < 1000; i++){ if(i % 3 == 0 || i % 5 == 0) ans += i; } cout << ans << endl; return 0;}
2.前n项求和公式思路:3或5的倍数之和 = 3的倍数之和 + 5的倍数之和 - 15的倍数之和(减去重复计入值)
1234567891011#include <iostream>using namespace std;int main(){ //时间复杂度:O(4) int t3 = (3 + 999) * 333 / 2; int t5 = (5 + 995) * 199 / 2; int t15 = (15 + 990) * 66 / 2; cout << t3 + t5 - t15 <<endl; return 0;}