Consider a spiral in which, starting from 1 in the center, we will successively arrange the numbers clockwise until spiral 5 is 5:
You can check that the sum of all the numbers on the diagonals is 101.
What will be the sum of the numbers on the diagonals, for a spiral size of 1195 by 1195?
function task1(size) { for (var v=1, d=2, sum=v; d<size; v+=4*d, d+=2) { sum += 4*v+10*d; } return sum; }
function task1_alt(size) { return (size * size * size * 2 / 3.0 + size * size / 2.0 + size * 4 / 3.0 - 1.5); }
We define the function P (n, k) as follows: P (n, k) = 1, if n can be represented as a sum of k prime numbers (prime numbers in the record can be repeated, 1 is not a prime number) and P (n, k) = 0 otherwise.
For example, P (10,2) = 1, since 10 can be represented as a sum of 3 + 7 or 5 + 5, and P (11,2) = 0, since no two prime numbers can give a total of 11.
We define the function S (n) as the sum of the values of the function P (i, k) for all i and k such that 1 <= i <= n, 1 <= k <= n.
Thus, S (2) = P (1,1) + P (2,1) + P (1,2) + P (2,2) = 1, S (10) = 20, S (1000) = 248838.
Find the S (17688).
// function isPrime(x,primes){ for (var j=0, len=primes.length; j<len; j++){ if (x%primes[j]==0) return false; } return true; }
function task2(n) { for (var i=4, count=2, primes=[2,3]; i<=n; i++){ // - if (i%2==0) { count+=i/2-1; } else { // count+=(i-1)/2-1; // if (i!=(primes[primes.length-1]+2)) { count--; } // - if (isPrime(i,primes)) { count++; // primes[primes.length]=i; } } } return count; }
The number 125874 and the result of multiplying it by 2 - 251748 can be obtained from each other by rearranging the numbers. Find the smallest positive positive x such that the numbers 2 * x, 3 * x can be obtained from each other by rearranging the digits.
// function sortVal(val) { var str=val.toString(); if (str.length>1){ return str.split("").sort().join(""); } else { return str; } }
function task3(a,b) { // x x var xFirstMax = Math.floor(9/Math.max(a,b)); for (var x=1, xSorted=''; x<=1000000; x++){ // x, if (x.toString()[0]>xFirstMax) continue; // x xSorted=sortVal(x); // x a b if (xSorted==sortVal(a*x) && xSorted==sortVal(b*x)) { var founded=x; break; } } return founded; }
If we take 47, turn it upside down and add it, we get 47 + 74 = 121 - a palindrome number.
If you take 349 and do this operation on it three times, you will also get a palindrome:
349 + 943 = 1292
1292 + 2921 = 4213
4213 + 3124 = 7337
Find the number of positive positive integers less than 13110 such that it is impossible to get a palindrome from them in 50 or less applications of the described operation (the operation must be applied at least once).
function task4(maxNum) { for (var num=1, count=0; num<maxNum; num++){ // 50 for (var op = 1, val=num, palindrome=false; op<=50; op++) { val=val.toString(); // , for (var iMax=val.length-1, i=iMax, j=0, digSum=0, arrSum=[], carry=0; i>=0 && j<=iMax; i--, j++) { // + digSum = parseInt(val[i])+parseInt(val[j])+carry; // 9 if (digSum>9) { // 10 arrSum[arrSum.length]=digSum-10; // carry=1; // if (i==0) arrSum[arrSum.length]=1; } else { arrSum[arrSum.length]=digSum; carry=0; } } val = arrSum.join(""); // if (val == val.split("").reverse().join("")) { // - palindrome = true; break; } } // - if (!palindrome) { count++; } } return count; }
Consider all possible numbers a b for 1 <a <6 and 1 <b <6:
2 2 = 4, 2 3 = 8, 2 4 = 16 , 2 5 = 32 3 2 = 9, 3 3 = 27, 3 4 = 81, 3 5 = 243, 4 2 = 16 , 4 3 = 64, 4 4 = 256, 4 5 = 1024, 5 2 = 25, 5 3 = 125, 5 4 = 625, 5 5 = 3125
If we remove the repetition, we get 15 different numbers.
How many different numbers a b for 2 <a <149 and 2 <b <121?
// function isPowerOf(val,prime){ var power = 0; // if (val%prime == 0) { // while ((val/=prime)>=1) { power++; if (val==1) return power; } } return 0; }
// a1, b1 - , a2, b2 - function task5(a1,a2,b1,b2) { for (var a=a1+1, arr=[], power=1, count=0; a<a2; a++){ // var primes = [2,3,5,7,11]; // , , for (var i=0; i < primes.length; i++) { power = isPowerOf(a,primes[i]); if (power>1) { break; } } for (var b=b1+1, idx=''; b<b2; b++){ // a b idx = (power>1)? "a"+primes[i]+"b"+power*b : "a"+a+"b"+b ; // if (!arr[idx]) { // arr[idx]=1; // count++; } } } return count; }
In some numbers, you can find sequences of numbers, which together give 10. For example, in the number 3523014, there are four such sequences:
352 3014
3,523,014
3 5230 14
35 23014
You can find such wonderful numbers, each digit of which is included in at least one such sequence.
For example, 3523014 is a remarkable number, and 28546 is not (there is no sequence of numbers in it, giving a total of 10 and including 5).
Find the number of these remarkable numbers in the interval [1, 1900000] (both boundaries - inclusive).
// "" function isWonderful(num) { var arrRaw=[], arrPrep=[], len=0, sum=0; // arrRaw = num.toString().split(""); for (var i=0, digit=0; i<arrRaw.length; i++){ digit=parseInt(arrRaw[i]); // if (digit>0) { arrPrep[arrPrep.length]=digit; // sum+=digit; } } // len=arrPrep.length; // 10, - "" if (sum==10) { return true; // 10 - "" } else if (sum<10) { return false; } else { // 2 2 10 - "" if ((arrPrep[0]+arrPrep[1])>10 || (arrPrep[len-1]+arrPrep[len-2])>10) { return false; } for (var i=0, s=[]; i<len; i++){ // s[i]=[]; // s[i][i]=arrPrep[i]; } // for (var i=0, arrCheck=[]; i<len-1; i++){ for (var j=i; j<len-1; j++){ // s[i][j+1]=s[i][j]+s[j+1][j+1]; // if (s[i][j+1]==10){ for (var k=i;k<=j+1;k++){ // , arrCheck[k]=s[k][k]; } break; // - } else if (s[i][j+1]>10) { break; } } } // if (arrPrep.join('')==arrCheck.join('')) { // - "" return true; } } return false; }
// "" x1, x2 function task6(x1,x2) { for (var x=x1, count=0; x<=x2; x++){ // "" - if (isWonderful(x)) { count++; } } return count; }
Source: https://habr.com/ru/post/312278/
All Articles