Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 102 题:不用加减乘除运算符,求整数的7倍 #161

Open
yygmind opened this issue Jul 10, 2019 · 56 comments
Open

第 102 题:不用加减乘除运算符,求整数的7倍 #161

yygmind opened this issue Jul 10, 2019 · 56 comments

Comments

@yygmind
Copy link
Contributor

yygmind commented Jul 10, 2019

No description provided.

@noctiomg
Copy link

可以使用三类方式:位运算加法、JS hack、进制转换。实现方式分别如下:

/* -- 位运算 -- */

// 先定义位运算加法
function bitAdd(m, n){
    while(m){
        [m, n] = [(m & n) << 1, m ^ n];
    }
    return n;
}

// 位运算实现方式 1 - 循环累加7次
let multiply7_bo_1 = (num)=>
{
  let sum = 0,counter = new Array(7); // 得到 [empty × 7]
  while(counter.length){
    sum = bitAdd(sum, num);
    counter.shift();
  }
  return sum;
}

// 位运算实现方式 2 - 二进制进3位(乘以8)后,加自己的补码(乘以-1)
let multiply7_bo_2 = (num) => bitAdd(num << 3, -num) ;

/* -- JS hack -- */

// hack 方式 1 - 利用 Function 的构造器 & 乘号的字节码
let multiply7_hack_1 = (num) => 
    new Function(["return ",num,String.fromCharCode(42),"7"].join(""))();

// hack 方式 2 - 利用 eval 执行器 & 乘号的字节码
let multiply7_hack_2 = (num) => 
		eval([num,String.fromCharCode(42),"7"].join(""));

// hack 方式 3 - 利用 SetTimeout 的参数 & 乘号的字节码
setTimeout(["window.multiply7_hack_3=(num)=>(7",String.fromCharCode(42),"num)"].join(""))

/* -- 进制转换 -- */

// 进制转换方式 - 利用 toString 转为七进制整数;然后末尾补0(左移一位)后通过 parseInt 转回十进制
let multiply7_base7 = 
    (num)=>parseInt([num.toString(7),'0'].join(''),7);

贴一个自己之前发布过的这个题的解法和思路逻辑:
frontend9/fe9-library#272

@momodiy
Copy link

momodiy commented Jul 10, 2019

const sevenTimes = num => eval(new Array(7).fill(num).join('+'))

sevenTimes(10) // 70

@yywangyu
Copy link

涨姿势

@zhihaozhang
Copy link

const sevenTimes = num => new Array(7).fill(num).reduce((p,v)=> p+v)

sevenTimes(10) // 70

you used + , which is not allowed.

@EnergySUD
Copy link

function getNum(x){ let a = Array(x << 3).fill(1);a.splice(0,x); return a.length; } console.log(getNum(2)) // 14

@momodiy
Copy link

momodiy commented Jul 10, 2019

const sevenTimes = num => new Array(7).fill(num).reduce((p,v)=> p+v)

sevenTimes(10) // 70

you used + , which is not allowed.

You are right...

@ksora94
Copy link

ksora94 commented Jul 10, 2019

仅供娱乐

function nTimes(m, n) {
	const con = document.createElement('div');
	const child = document.createElement('div');

	con.style.setProperty('width', `${m}px`);
	child.style.setProperty('width', `${n}00%`);
	con.appendChild(child);
	document.body.appendChild(con);

	setTimeout(() => document.body.removeChild(con));
	
	return child.clientWidth;
}

nTimes(10, 7); // 70

@qdlaoyao
Copy link

qdlaoyao commented Jul 10, 2019

''.padEnd(5, 0).replace(/0/g, ''.padEnd(7, 0)).length // 5 * 7 = 35
正则实现数学运算

@zhangenming
Copy link

zhangenming commented Jul 10, 2019

var multip0 = num => ''.padEnd(num).repeat(7).length
var multip1 = num => [0, 7, 14, /*我有一个绝佳的想法, 可惜这里地方太小写不下...*/][num]
var multip2 = num =>new Array(num).fill().map(()=> new Array(7).fill()).flat().length

@supermanklk
Copy link

function getNum(num, count) { return eval(new Array(count).fill(num).join('+')); } getNum(7,3) // 21 getNum(7,4) // 28 借鉴楼层 比较高明. 学习了~

@murrayee
Copy link

虽然这样做没什么技术含量,但至少符合题意吧哈哈
function(x){
let arr = Array(x)
let resultArr = [...arr,...arr,...arr,...arr,...arr,...arr,...arr]
return resultArr.length
}

秀儿 是你吗

@delaiwen666
Copy link

delaiwen666 commented Jul 10, 2019

function multiple(data){
var v = new Array(Number(data.num)+1).join('*')
var mul= data.multiple-1
console.log(v,mul)
return RegExp('^((.)(\2{'+mul+'}))+$','gm').test(v)
}
multiple({
multiple:7,
num:'77'
})

@Weathers0086
Copy link

function bit_add(a, b){
  var carry = a & b;
  var noCarrySum = a ^ b;
  if(carry)
    return bit_add(carry << 1, noCarrySum);
  else
    return noCarrySum;
}
function bit_multiply7(a){
  return bit_add(a<<2, bit_add(a<<1, a));
}
bit_multiply7(999999)

@zwtgit
Copy link

zwtgit commented Jul 11, 2019

虽然这样做没什么技术含量,但至少符合题意吧哈哈
function(x){
let arr = Array(x)
let resultArr = [...arr,...arr,...arr,...arr,...arr,...arr,...arr]
return resultArr.length
}

鬼才哈哈

@630268501
Copy link

var ab = [];
function seven(n,ten){
if(!n){
ten&&ten==1&&ab.unshift(1);
ten&&ten==2&&ab.unshift(2);
ten&&ten==3&&ab.unshift(3);
ten&&ten==4&&ab.unshift(4);
ten&&ten==5&&ab.unshift(5);
ten&&ten==6&&ab.unshift(6);
}
if(n[n.length-1]==0){
!ten&&ab.unshift(0)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(1)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(2)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(3)&&seven(n.slice(0,n.length-1));
ten&&ten==4&&ab.unshift(4)&&seven(n.slice(0,n.length-1));
ten&&ten==5&&ab.unshift(5)&&seven(n.slice(0,n.length-1));
ten&&ten==6&&ab.unshift(6)&&seven(n.slice(0,n.length-1));
}else if(n[n.length-1]==1){
!ten&&ab.unshift(7)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(8)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(9)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(0)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(1)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(2)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(3)&&seven(n.slice(0,n.length-1),1);
}else if(n[n.length-1]==2){
!ten&&ab.unshift(4)&&seven(n.slice(0,n.length-1),1);
ten&&ten==1&&ab.unshift(5)&&seven(n.slice(0,n.length-1),1);
ten&&ten==2&&ab.unshift(6)&&seven(n.slice(0,n.length-1),1);
ten&&ten==3&&ab.unshift(7)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(8)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(9)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(0)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==3){
!ten&&ab.unshift(1)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(2)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(3)&&seven(n.slice(0,n.length-1),2);
ten&&ten==3&&ab.unshift(4)&&seven(n.slice(0,n.length-1),2);
ten&&ten==4&&ab.unshift(5)&&seven(n.slice(0,n.length-1),2);
ten&&ten==5&&ab.unshift(6)&&seven(n.slice(0,n.length-1),2);
ten&&ten==6&&ab.unshift(7)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==4){
!ten&&ab.unshift(8)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(9)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(0)&&seven(n.slice(0,n.length-1),3);
ten&&ten==3&&ab.unshift(1)&&seven(n.slice(0,n.length-1),3);
ten&&ten==4&&ab.unshift(2)&&seven(n.slice(0,n.length-1),3);
ten&&ten==5&&ab.unshift(3)&&seven(n.slice(0,n.length-1),3);
ten&&ten==6&&ab.unshift(4)&&seven(n.slice(0,n.length-1),3);
}
}
写不下去了,我感觉在下头发都在摇曳了,胸口憋了一口闷气。

@guqianfeng
Copy link

const sevenTimes = num => eval(new Array(7).fill(num).join('+'))

sevenTimes(10) // 70
  • 加号字符串不算加号运算符,那我是不是可以。。。eval(${num}*7)

@yucopowo
Copy link

yucopowo commented Jul 11, 2019

左移3位 相当于乘8 然后再减去自己 不就是7倍了吗 减法的移位运算可以转换加法的移位运算

整数: n

背景知识: -n = ~(n-1) = ~n+1

转换方程

n*7 ==> n<<3-n ==> add(n<<3, ~n+1) ==> add(n<<3, add(~n, 1))

function add(m,n){ 
     return n?add(m^n, (m&n)<<1):m; 
}

add(n<<3,  add(~n, 1))
14

通用场景

不用加减乘除运算符,求整数n的m倍

这个就不难了 哈哈

@pengcc
Copy link

pengcc commented Jul 11, 2019

相当于7xn个位数
const getSum = (x) => Array.from({ length: 7 }).fill(Array.from({ length: x }, item => 1).join('')).join('').length;

@630268501
Copy link

function(n){
Math.abs(~eval([n<<3,~n].join("")))
}
第二个答案,一长串的是第一个答案,还有个构想是用Math.random(),但没写出来。建议写个接口,然后大数据爬出答案,return出来。

@630268501
Copy link

function qq(n){
var a = n;
for(var i=0;i<6;i=Math.abs(~i)){
for(var j=0;j<a;j=Math.abs(~j)){
n = Math.abs(~n)
}
}
console.log(n);
}

@libin1991
Copy link

libin1991 commented Jul 12, 2019

所有的答案这个最优秀: @Huangyanyang

function Sum(x){
    let arr = Array(x)
    let resultArr = [...arr,...arr,...arr,...arr,...arr,...arr,...arr]
    return resultArr.length
}

@J-DuYa
Copy link

J-DuYa commented Jul 12, 2019

突然觉得这些好有意思✈😄 学到了🙆‍

function getSevenNum(n = 0) { return (n << 3) - n }

测试机😝

getSevenNum() // 0
getSevenNum(1) // 7
getSevenNum(11) //77

@EnergySUD
Copy link

突然觉得这些好有意思✈ 学到了🙆‍

function getSevenNum(n = 0) { return (n << 3) - n }

测试机

getSevenNum() // 0
getSevenNum(1) // 7
getSevenNum(11) //77

不能出现加减乘除,如果你这可以,不如直接 7*n ....

@J-DuYa
Copy link

J-DuYa commented Jul 12, 2019

突然觉得这些好有意思✈ 学到了🙆‍

function getSevenNum(n = 0) { return (n << 3) - n }

测试机

getSevenNum() // 0
getSevenNum(1) // 7
getSevenNum(11) //77

不能出现加减乘除,如果你这可以,不如直接 7*n ....
好的 没注意 抱歉

@ellis-s
Copy link

ellis-s commented Jul 12, 2019

var ab = [];
function seven(n,ten){
if(!n){
ten&&ten==1&&ab.unshift(1);
ten&&ten==2&&ab.unshift(2);
ten&&ten==3&&ab.unshift(3);
ten&&ten==4&&ab.unshift(4);
ten&&ten==5&&ab.unshift(5);
ten&&ten==6&&ab.unshift(6);
}
if(n[n.length-1]==0){
!ten&&ab.unshift(0)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(1)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(2)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(3)&&seven(n.slice(0,n.length-1));
ten&&ten==4&&ab.unshift(4)&&seven(n.slice(0,n.length-1));
ten&&ten==5&&ab.unshift(5)&&seven(n.slice(0,n.length-1));
ten&&ten==6&&ab.unshift(6)&&seven(n.slice(0,n.length-1));
}else if(n[n.length-1]==1){
!ten&&ab.unshift(7)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(8)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(9)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(0)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(1)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(2)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(3)&&seven(n.slice(0,n.length-1),1);
}else if(n[n.length-1]==2){
!ten&&ab.unshift(4)&&seven(n.slice(0,n.length-1),1);
ten&&ten==1&&ab.unshift(5)&&seven(n.slice(0,n.length-1),1);
ten&&ten==2&&ab.unshift(6)&&seven(n.slice(0,n.length-1),1);
ten&&ten==3&&ab.unshift(7)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(8)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(9)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(0)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==3){
!ten&&ab.unshift(1)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(2)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(3)&&seven(n.slice(0,n.length-1),2);
ten&&ten==3&&ab.unshift(4)&&seven(n.slice(0,n.length-1),2);
ten&&ten==4&&ab.unshift(5)&&seven(n.slice(0,n.length-1),2);
ten&&ten==5&&ab.unshift(6)&&seven(n.slice(0,n.length-1),2);
ten&&ten==6&&ab.unshift(7)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==4){
!ten&&ab.unshift(8)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(9)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(0)&&seven(n.slice(0,n.length-1),3);
ten&&ten==3&&ab.unshift(1)&&seven(n.slice(0,n.length-1),3);
ten&&ten==4&&ab.unshift(2)&&seven(n.slice(0,n.length-1),3);
ten&&ten==5&&ab.unshift(3)&&seven(n.slice(0,n.length-1),3);
ten&&ten==6&&ab.unshift(4)&&seven(n.slice(0,n.length-1),3);
}
}
写不下去了,我感觉在下头发都在摇曳了,胸口憋了一口闷气。

哈哈哈哈 笑死我了。。。

@bee0060
Copy link

bee0060 commented Jul 13, 2019

算是参考前面的,也不知道是不是已经有这个办法了。

const seven = n => new Array(n<<3).slice(n).length;

@stanleylu3216
Copy link

stanleylu3216 commented Jul 14, 2019

轉乘 7 進位向左位移, 再轉回 10 進位

function fn(num) {
  var d = (num).toString(7)
  return parseInt(`${d}0` , 7)
}

fn(123) => 861

@weiweixuan
Copy link

常量 sevenTimes  =  NUM  =>  的eval(新 阵列(7)。填充(NUM)。加入(' + '))

sevenTimes(10)// 70

方法很不错哈,但是使用了+,不然就完美了

@shijianzhong
Copy link

// 定义数字0:
var zero = function (f) {
return function (x) {
return x;
}
};

// 定义数字1:
var one = function (f) {
return function (x) {
return f(x);
}
};

// 定义加法:
function add(n, m) {
return function (f) {
return function (x) {
return m(f)(n(f)(x));
}
}
}

// 计算数字2 = 1 + 1:
var two = add(one, one);

// 计算数字3 = 1 + 2:
var three = add(one, two);

// 计算数字5 = 2 + 3:
var five = add(two, three);

// 你说它是3就是3,你说它是5就是5,你怎么证明?

// 呵呵,看这里:

// 给3传一个函数,会打印3次:
(three(function () {
console.log('print 3 times');
}))();

// 给5传一个函数,会打印5次:
(five(function () {
console.log('print 5 times');
}))();

@shijianzhong
Copy link

function seventimes(x){
let tmp=new Array(x);
let resul = tmp.concat(tmp,tmp,tmp,tmp,tmp,tmp);
return resul.length
}

@lieaqi
Copy link

lieaqi commented Jul 16, 2019

var x = [].concat(...([...new Array(num)].map(x => [...new Array(7)])))
console.log(x.length)

@linlinyang
Copy link

linlinyang commented Jul 17, 2019

`

    /* 
    * 按位加
     */
    function add(num1,num2){
        return num2 ? add(num1 ^ num2,(num1 & num2) << 1) : num1;
    }

    /* 
    * 乘以8减一
    */
    function multiple(num){
        return add(num << 3,add(~num,1));
    }
    console.log(multiple(5));`

@cryst4lMao
Copy link

var ab = [];
function seven(n,ten){
if(!n){
ten&&ten==1&&ab.unshift(1);
ten&&ten==2&&ab.unshift(2);
ten&&ten==3&&ab.unshift(3);
ten&&ten==4&&ab.unshift(4);
ten&&ten==5&&ab.unshift(5);
ten&&ten==6&&ab.unshift(6);
}
if(n[n.length-1]==0){
!ten&&ab.unshift(0)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(1)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(2)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(3)&&seven(n.slice(0,n.length-1));
ten&&ten==4&&ab.unshift(4)&&seven(n.slice(0,n.length-1));
ten&&ten==5&&ab.unshift(5)&&seven(n.slice(0,n.length-1));
ten&&ten==6&&ab.unshift(6)&&seven(n.slice(0,n.length-1));
}else if(n[n.length-1]==1){
!ten&&ab.unshift(7)&&seven(n.slice(0,n.length-1));
ten&&ten==1&&ab.unshift(8)&&seven(n.slice(0,n.length-1));
ten&&ten==2&&ab.unshift(9)&&seven(n.slice(0,n.length-1));
ten&&ten==3&&ab.unshift(0)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(1)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(2)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(3)&&seven(n.slice(0,n.length-1),1);
}else if(n[n.length-1]==2){
!ten&&ab.unshift(4)&&seven(n.slice(0,n.length-1),1);
ten&&ten==1&&ab.unshift(5)&&seven(n.slice(0,n.length-1),1);
ten&&ten==2&&ab.unshift(6)&&seven(n.slice(0,n.length-1),1);
ten&&ten==3&&ab.unshift(7)&&seven(n.slice(0,n.length-1),1);
ten&&ten==4&&ab.unshift(8)&&seven(n.slice(0,n.length-1),1);
ten&&ten==5&&ab.unshift(9)&&seven(n.slice(0,n.length-1),1);
ten&&ten==6&&ab.unshift(0)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==3){
!ten&&ab.unshift(1)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(2)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(3)&&seven(n.slice(0,n.length-1),2);
ten&&ten==3&&ab.unshift(4)&&seven(n.slice(0,n.length-1),2);
ten&&ten==4&&ab.unshift(5)&&seven(n.slice(0,n.length-1),2);
ten&&ten==5&&ab.unshift(6)&&seven(n.slice(0,n.length-1),2);
ten&&ten==6&&ab.unshift(7)&&seven(n.slice(0,n.length-1),2);
}else if(n[n.length-1]==4){
!ten&&ab.unshift(8)&&seven(n.slice(0,n.length-1),2);
ten&&ten==1&&ab.unshift(9)&&seven(n.slice(0,n.length-1),2);
ten&&ten==2&&ab.unshift(0)&&seven(n.slice(0,n.length-1),3);
ten&&ten==3&&ab.unshift(1)&&seven(n.slice(0,n.length-1),3);
ten&&ten==4&&ab.unshift(2)&&seven(n.slice(0,n.length-1),3);
ten&&ten==5&&ab.unshift(3)&&seven(n.slice(0,n.length-1),3);
ten&&ten==6&&ab.unshift(4)&&seven(n.slice(0,n.length-1),3);
}
}
写不下去了,我感觉在下头发都在摇曳了,胸口憋了一口闷气。

佩服佩服,是什么驱使着你

@liuwenai
Copy link

我太菜了🙂

@qiannianchong25
Copy link

轉乘 7 進位向左位移, 再轉回 10 進位

function fn(num) {
  var d = (num).toString(7)
  return parseInt(`${d}0` , 7)
}

fn(123) => 861

估计这就是作者出这题的意图了。就是考虑进制转换。不过,需要指出的是,Number的toString方法,参数值最终为2到36的整数(不符合的会报错。小数会取整,字符串会转成数字),估计是考虑到数字和字母的组合吧。

@wave52
Copy link

wave52 commented Aug 2, 2019

// 不用四则运算
// 加法:
// 递归实现
function add(m, n){ 
  return n ? add(m^n, (m & n)<<1) : m; 
}
// 迭代实现
function add2(m, n){
  while(m) { // 直到没有进位
    [m, n] = [(m & n) << 1, m ^ n];
  }
  return n;
}
// 乘法:
// 整数n的m倍
function multi(n, m){
  var i = 0
  var res  = 0;
  while (m != 0) { // 乘数为0则结束
    // 处理乘数当前位
    if((m & 1) === 1){
      res = add(res, n<<i);
      m = m>>1;
      i = add(1, i); // i记录当前位是第几位
    }else{
      m = m>>1;
      i = add(1, i);
    }
  }
  return res;
}

@xgqfrms
Copy link

xgqfrms commented Aug 14, 2019

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-08-14
 *
 * @description auto-sevent-times-without-math.js
 * @description 不用加减乘除运算符, 求整数的7倍
 * @description js array get sum value without call math methods
 * @augments
 * @example eval([1,2,3].join('+'));// 6
 * @link https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers
 *
 */

let log = console.log;

const autoSevenTimes = (int = 0, times = 7, debug = false) => {
    let result = [];
    if (!int) {
        return 0;
    } else {
        for (let i = 0; i < times; i++) {
            result.push(int);
        }
    }
    // result = result.reduce((acc, item) => acc + item);
    result = eval(result.join(`+`));
    if(debug) {
        log(`result = `, result);
    }
    return result;
};

let num = 3;
autoSevenTimes(num);
// expect: 3 * 7 = 21

bitwise-operator

https://repl.it/@xgqfrms/bitwise-operator-and-left-shift-and-right-shift

const autoSeventTimes = (num = 0, times = 7) => {
    let x = Math.floor(times / 2);
    return (num << x) - num;
};

let xyz = autoSeventTimes(3);
// 21

console.log(`xyz =`, xyz);

@xgqfrms
Copy link

xgqfrms commented Aug 14, 2019

var x = [].concat(...([...new Array(num)].map(x => [...new Array(7)])))
console.log(x.length)

typed array

I think using typed array is more meaningful.

const result = [].concat(...([...new Uint8Array(num).map(item => item = 1)].map(x => [...new Uint8Array(7).map(item => item = 1)])));

console.log(result, result.length);

@DarthVaderrr
Copy link

涨姿势

@yangxinSamsara
Copy link

yangxinSamsara commented Aug 30, 2019

let a="aaaaa"
let b=a.repeat(7)
console.log(b.length)

@yaodongyi
Copy link

全都是大佬,全是二进制。

function seven(num, len) {
  return Array.from({ length: len }).fill(Array.from({ length: num }).fill(num)).flat().length;
}
console.log(seven(3, 7));

@Hunterang
Copy link

剽窃智慧的感觉真的很爽,感觉天才好多,大约总结出6种方法1、就是位运算加法;2、eval处理方法(fromCharCodeAt(42),以及Array(7).fill(num))3、进制转转换,数字转为7进制进一位在转为10进制 4、数组方法,根据位运算左移三位,则是8倍的数字,出去本身就是7倍的数组,返回length ,5、竟然想到dom元素子结点宽度设置倍数的关系。6、 循环7次Array(m)的数组concat,点赞为各位

@onloner2012
Copy link

虽然这样做没什么技术含量,但至少符合题意吧哈哈
function(x){
let arr = Array(x)
let resultArr = [...arr,...arr,...arr,...arr,...arr,...arr,...arr]
return resultArr.length
}
我去啊,最原始的方法啊,数个数,哈哈哈

@tywei90
Copy link

tywei90 commented Dec 8, 2019

const times7 = x => '1'.repeat(x).repeat(7).length

@yaodongyi
Copy link

yaodongyi commented Jan 26, 2020

..... 考基础 😊

let num = 2;
Array(7).fill(Array.from({ length: num })).flat().length;

@robynluo
Copy link

robynluo commented Jun 3, 2020

const num = 5;
Array.from(Array(7)).fill(Array.from(Array(num))).flat().length

@ay233
Copy link

ay233 commented Jun 10, 2020

const getTimes=(num,time)=> "1".repeat(num).repeat(time).length;

@bighamD
Copy link

bighamD commented Jun 11, 2020

var n = 10
Array.from({length: 7}).fill(n).reduce((a, b) => a+b, 0)

@wxf-start
Copy link

function setNumber(number){
return Math.imul(number,7)
}
setNumber(6)

@chenyanfei-m
Copy link

chenyanfei-m commented Jul 25, 2020

while取余7次

var a = 3
var foo = 0

for(let i = 0; i <= Infinity; i++){
    if(i % a === 0) foo +=1
    if(foo === 8){
        console.log(i)
        break
    }
}

@rudyxu1102
Copy link

const add = (a, b) => {
    if (a === 0) return b
    if (b === 0) return a
    return add(a ^ b, (a & b) << 1)
}

const sum = (n) => {
    return add(n << 3, -n)
}

console.log(sum(9))

@jaan1025
Copy link

const calc  = num => Array.from({ length: 7 }).reduce((ret, cur) => ret.concat(Array.from({ length: num })), []).length;

@waldonUB
Copy link

位运算 + 剔除数组

  • 好记且简单

思路

  • 位运算 m << 3,相当于乘以8
  • 用这个来创建一个数组,然后剔除掉自己的长度即可
  • 剔除方式为splice(0, m),或者是循环然后pop()

实现

const addFnByBit = function (m) {
  const res = new Array(m << 3)
  for (let i = 0; i < m; i++) {
    res.pop()
  }
  // res.splice(0, m)
  return res.length
}

console.log(addFnByBit(5))

@pixinlei
Copy link

let resultArr = [...arr,...arr,...arr,...arr,...arr,...arr,...arr]
return resultArr.length
}

秀儿 是你

太强了

@XW666
Copy link

XW666 commented Aug 22, 2022

 function bitAdd(num) {
    return Array.from({
      length: 7
    }).fill(num).reduce((p, v) => p + v)
  }

@zhaocchen
Copy link

zhaocchen commented Mar 15, 2024

思路:将此整数先左移三位(*8)然后再减去原值:X << 3 – X。

function multipleOfSeven(x) {
  return (x << 3) - x; 
}

for (let i = 1; i < 10; i++) {
  console.log(i, multipleOfSeven(i));
}

// 1 7
// 2 14
// 3 21
// 4 28
// 5 35
// 6 42
// 7 49
// 8 56
// 9 63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests