# JS | 教练，我想做习题8

## 📖 题目1：N的尾随零数！

N！\= 1 * 2 * 3 * ... * N

``zeros(6) = 1# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zerozeros(12) = 2``# 12! = 479001600 --> 2 trailing zeros``

``function zeros (n) {// your code here``}``

## 📖 题目2：整数：娱乐一

42的除数是：1、2、3、6、7、14、21、42。这些除数的平方是：1、4、9、36、49、196、441、1764。平方除数的总和是2500，这是50 * 50，一个正方形！

``list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]``list_squared(42, 250) --> [[42, 2500], [246, 84100]]``

``function listSquared(m, n) {// your code``}``

## 📖 题目3：双可乐

Sheldon，Leonard，Penny，Rajesh和Howard正在自动售货机排队购买“ Double Cola”饮料。队列中没有其他人。队列中的第一个（Sheldon）买了一个罐子，喝下后加倍！由此产生的两个谢尔顿进入队列的末端。然后，队列中的下一个（Leonard）买了一个罐头，喝了一下，然后作为两个Leonards到达队列的末端，依此类推。

``Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny``

``whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 1) == "Sheldon"whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 52) == "Penny"``whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 7230702951) == "Leonard"``

``function whoIsNext(names, r){//your code here``}``

## 答案

### 🍗 题目1的答案

``function zeros (n) {var zs = 0;while(n>0){n=Math.floor(n/5);zs+=n}return zs;``}``

``function zeros(n) {return n/5 < 1 ? 0 : Math.floor(n/5) + zeros(n/5);``}``

``function zeros (n) {var res = 0;for(var i=5; i<n; i*=5)res += Math.floor(n/i);return res;``}``

``function zeros(n){ //trailing zeroes of n!var countfives = 0,pow5 = 1,occurances;while(pow5 *= 5, occurances = Math.floor(n/pow5)) countfives += occurances;return countfives;``}``

``function zeros (n) {n = ~~(n/5);return  n + (n<5 ? 0 : zeros(n));``}``

### 🍗 题目2的答案

``function listSquared (m, n) {var matches = [];for (var i = m; i <= n; ++i) {var sum = getDivisors(i).reduce((sum, n) => sum + n * n, 0);var ok = Number.isInteger(Math.sqrt(sum));if (ok) {matches.push([i, sum]);}}return matches;}function getDivisors (n) {var divisors = [];for (var i = 1; i <= n / 2; ++i) {if (n % i) {continue;}divisors.push(i);}return divisors.concat([n]);``}``

``function listSquared(m, n) {var arr = [];for (var i = m; i <= n; i++){var temp = 0;for (var j = 1; j <= i; j++) {if ( i % j == 0) temp += j*j;};if ( Math.sqrt(temp) % 1 == 0) arr.push([i, temp]);};return arr;``}``

``const cache = new Map();const square = x => x * x;const sum = (x, y) => x + yconst isSquareDivisor = (x) => Math.sqrt(x) % 1 === 0;const findDivisors = n => {if (cache.has(n)) return cache.get(n);const divisors = n === 1 ? [1] : [1, n];for (let i = 2; i < n; i++) {if (n % i === 0)divisors.push(i);}cache.set(n, divisors);return divisors;}function listSquared(m, n) {const results = [];for (let i=m; i <=n; i++) {const divisors = findDivisors(i)const squared = divisors.map(square)const summed = squared.reduce(sum, 0)if (isSquareDivisor(summed)) {results.push([i, summed])}}return results;``}``

``function listSquared(m,n){let chamber = {};function wuTang(x){let i = 1, odb = [], meth;while (i <= x){!(x % i) ? (odb.push(i),i++) : i++}meth = odb.reduce((a,e) => (chamber[e] ? chamber[e] : (chamber[e] = e * e, chamber[e])) + a,0);return !(Math.sqrt(meth) % 1) ? [--i,meth] : null;}return Array.from({length:(n - m)},(e,i) => wuTang(i + m)).filter(e => e);``}``

``const c = [[1,1],[42,2500],[246,84100],[287,84100],[728,722500],[1434,2856100],[1673,2856100],[1880,4884100],[4264,24304900],[6237,45024100],[9799,96079204],[9855,113635600]];``const listSquared = (a,b) => c.filter(([d]) => d>=a && d<=b);``

### 🍗 题目3的答案

``function whoIsNext(names, r) {var l = names.length;while (r >= l) { r -= l; l *= 2; }return names[Math.ceil(names.length * r / l)-1];``}``

``function whoIsNext(names, r){var numOfGeeks = names.length;var loga = Math.log((r/numOfGeeks)+1) / Math.log(2);var completeCycles = Math.floor(loga)var fullCycleColas = (Math.pow(2,completeCycles)-1) * numOfGeeks;var currCycleSize  =  Math.pow(2,completeCycles)    * numOfGeeks;var geekCode = Math.ceil((r - fullCycleColas)/currCycleSize * numOfGeeks);return names[geekCode-1]``}``

``function whoIsNext(names, r) {var peopleInLine = names.length;var copiesOfEachPerson = 1;while (r > peopleInLine) {r -= peopleInLine;copiesOfEachPerson *= 2;peopleInLine *= 2;}return names[Math.floor((r - 1) / copiesOfEachPerson) % peopleInLine];``}``

``function whoIsNext(names, n){x=names.length;i=1;while (n > x){n -= x;x *= 2;i *= 2;}return (names[parseInt((n - 1)/i)]);``}``

``function whoIsNext(names, r){var l = names.length, ini = l;while(r > ini) {r -= ini;ini *= 2;}return names[Math.ceil(r * l / ini) - 1];``}``

11 声望

4 粉丝

0 条评论

## 📖 题目1：N的尾随零数！

N！\= 1 * 2 * 3 * ... * N

``zeros(6) = 1# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zerozeros(12) = 2``# 12! = 479001600 --> 2 trailing zeros``

``function zeros (n) {// your code here``}``

## 📖 题目2：整数：娱乐一

42的除数是：1、2、3、6、7、14、21、42。这些除数的平方是：1、4、9、36、49、196、441、1764。平方除数的总和是2500，这是50 * 50，一个正方形！

``list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]``list_squared(42, 250) --> [[42, 2500], [246, 84100]]``

``function listSquared(m, n) {// your code``}``

## 📖 题目3：双可乐

Sheldon，Leonard，Penny，Rajesh和Howard正在自动售货机排队购买“ Double Cola”饮料。队列中没有其他人。队列中的第一个（Sheldon）买了一个罐子，喝下后加倍！由此产生的两个谢尔顿进入队列的末端。然后，队列中的下一个（Leonard）买了一个罐头，喝了一下，然后作为两个Leonards到达队列的末端，依此类推。

``Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny``

``whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 1) == "Sheldon"whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 52) == "Penny"``whoIsNext(["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"], 7230702951) == "Leonard"``

``function whoIsNext(names, r){//your code here``}``

## 答案

### 🍗 题目1的答案

``function zeros (n) {var zs = 0;while(n>0){n=Math.floor(n/5);zs+=n}return zs;``}``

``function zeros(n) {return n/5 < 1 ? 0 : Math.floor(n/5) + zeros(n/5);``}``

``function zeros (n) {var res = 0;for(var i=5; i<n; i*=5)res += Math.floor(n/i);return res;``}``

``function zeros(n){ //trailing zeroes of n!var countfives = 0,pow5 = 1,occurances;while(pow5 *= 5, occurances = Math.floor(n/pow5)) countfives += occurances;return countfives;``}``

``function zeros (n) {n = ~~(n/5);return  n + (n<5 ? 0 : zeros(n));``}``

### 🍗 题目2的答案

``function listSquared (m, n) {var matches = [];for (var i = m; i <= n; ++i) {var sum = getDivisors(i).reduce((sum, n) => sum + n * n, 0);var ok = Number.isInteger(Math.sqrt(sum));if (ok) {matches.push([i, sum]);}}return matches;}function getDivisors (n) {var divisors = [];for (var i = 1; i <= n / 2; ++i) {if (n % i) {continue;}divisors.push(i);}return divisors.concat([n]);``}``

``function listSquared(m, n) {var arr = [];for (var i = m; i <= n; i++){var temp = 0;for (var j = 1; j <= i; j++) {if ( i % j == 0) temp += j*j;};if ( Math.sqrt(temp) % 1 == 0) arr.push([i, temp]);};return arr;``}``

``const cache = new Map();const square = x => x * x;const sum = (x, y) => x + yconst isSquareDivisor = (x) => Math.sqrt(x) % 1 === 0;const findDivisors = n => {if (cache.has(n)) return cache.get(n);const divisors = n === 1 ? [1] : [1, n];for (let i = 2; i < n; i++) {if (n % i === 0)divisors.push(i);}cache.set(n, divisors);return divisors;}function listSquared(m, n) {const results = [];for (let i=m; i <=n; i++) {const divisors = findDivisors(i)const squared = divisors.map(square)const summed = squared.reduce(sum, 0)if (isSquareDivisor(summed)) {results.push([i, summed])}}return results;``}``

``function listSquared(m,n){let chamber = {};function wuTang(x){let i = 1, odb = [], meth;while (i <= x){!(x % i) ? (odb.push(i),i++) : i++}meth = odb.reduce((a,e) => (chamber[e] ? chamber[e] : (chamber[e] = e * e, chamber[e])) + a,0);return !(Math.sqrt(meth) % 1) ? [--i,meth] : null;}return Array.from({length:(n - m)},(e,i) => wuTang(i + m)).filter(e => e);``}``

``const c = [[1,1],[42,2500],[246,84100],[287,84100],[728,722500],[1434,2856100],[1673,2856100],[1880,4884100],[4264,24304900],[6237,45024100],[9799,96079204],[9855,113635600]];``const listSquared = (a,b) => c.filter(([d]) => d>=a && d<=b);``

### 🍗 题目3的答案

``function whoIsNext(names, r) {var l = names.length;while (r >= l) { r -= l; l *= 2; }return names[Math.ceil(names.length * r / l)-1];``}``

``function whoIsNext(names, r){var numOfGeeks = names.length;var loga = Math.log((r/numOfGeeks)+1) / Math.log(2);var completeCycles = Math.floor(loga)var fullCycleColas = (Math.pow(2,completeCycles)-1) * numOfGeeks;var currCycleSize  =  Math.pow(2,completeCycles)    * numOfGeeks;var geekCode = Math.ceil((r - fullCycleColas)/currCycleSize * numOfGeeks);return names[geekCode-1]``}``

``function whoIsNext(names, r) {var peopleInLine = names.length;var copiesOfEachPerson = 1;while (r > peopleInLine) {r -= peopleInLine;copiesOfEachPerson *= 2;peopleInLine *= 2;}return names[Math.floor((r - 1) / copiesOfEachPerson) % peopleInLine];``}``

``function whoIsNext(names, n){x=names.length;i=1;while (n > x){n -= x;x *= 2;i *= 2;}return (names[parseInt((n - 1)/i)]);``}``

``function whoIsNext(names, r){var l = names.length, ini = l;while(r > ini) {r -= ini;ini *= 2;}return names[Math.ceil(r * l / ini) - 1];``}``