当前位置:网站首页>特殊质数js实现
特殊质数js实现
2022-07-28 16:27:00 【梁毅】
首先我们看一下原题
7331是一个特殊的质数,因为我们去掉个位得到的733是一个质数;再次去掉个位得到的73又是一个质数;再去掉个位后得到的7依然是一个质数。对于形似这种的质数,我们称呼它为特殊质数。写一个程序对给定的待求特殊质数的位数 N (1≤N≤8)求出所有对应位数的特殊质数(注意:数字1不被看作一个质数)。输入包括一个整数,为待求特殊质数的位数 N。输出长度为N的特殊质数列表,每行一个。
看到这道题我们首先分析一下什么是质素。质数就是只能被一和自身整除的数,题目要求去掉各位之后仍然是是质数,所以最高位必须是质数,所以我们定义一个最高位满足条件的质数数组 var tiptop=[2,3,5,7];除了最高位后面的数那肯定是以奇数结尾的,但是以5结尾的数是能被5整除的,所以定义后面位数出现的数的数组为 var afterNum=[1,3,7,9] 。
首先我们写一个方法判断这个数是否是质数。代码如下
/** * 判断数是否是质素 * @param data 输入的数 * @returns {boolean} 输出是否是质素 */ function judgeZhiSu(data) { if (typeof data != 'number') { console.log("判断的对象不是数字"); return false; } if (data <= 0) { console.log("请输入大于0的正数"); return false; } if (data == 1) { return false; } if (!(/^[0-9]*[1-9][0-9]*$/.test(data.toString()))) { console.log('请输入整数'); return false; } var len = Math.floor(data / 2); for (var i = 2; i <= len; i++) { if (data % i == 0) { return false; } } return true; }
接着我们就要开始计算数据是否满足题目的条件,其中我们会在方法的内部定义一个递归方法
/** * 输入位数,输出满足条件的所有特殊质素 * @param len 位数长度 1<=len<=8 */ function computeMain(len) { if (typeof len != 'number') { console.log("判断的对象不是数字a"); return; } if (len < 1 || len > 8) { console.log("请输入大于等于1小于等于8的整数"); return; } if (!(/^[0-9]*[1-9][0-9]*$/.test(len.toString()))) { console.log('请输入整数'); return; } var dataArr = []; var tiptop = [2, 3, 5, 7]; var afterNum = [1, 3, 7, 9]; for (var i = 0, arrLen = tiptop.length; i < arrLen; i++) { recurrence(tiptop[i],1); } /** * 递归计算 * @param num 前几位满足条件的数据 * @param leve 已经多少位了 */ function recurrence(num, leve) { if(leve==len){ dataArr.push(num); return; } for(var i= 0,aLen=afterNum.length;i<aLen;i++){ if(judgeZhiSu((num+''+afterNum[i])*1)){ recurrence((num+''+afterNum[i])*1,leve+1) } } } return dataArr; }
边栏推荐
- Why do I choose to use go language?
- Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
- Vscode uses eslint prettier to format code automatically
- 【presto】presto 常用的命令
- Linear algebra and matrix theory (7)
- Iris framework practice of goweb development: project summary and review
- Encountered.Sqlite file processing during Android Development
- 2022 Niuke multi school second CDE
- Verilog 每日一题(VL26 简易秒表)
- Verilog 每日一题 (VL28 加减计数器)
猜你喜欢

Microservice Architecture - service registry and service gateway (6.8) (Reprint)

Basic principle of asynchronous FIFO (simple implementation based on Verilog)

Verilog daily question (vl14 vending machine 1 -- FSM common question types)

Verilog 每日一题(VL4 移位运算与乘法)

格雷码和二进制的转换及典型例题(4bits格雷码计数器)

Goweb开发之Beego框架实战:第四节 数据库配置及连接

Goweb开发之Beego框架实战:第五节 项目搭建及注册用户

技术面轻松通过,HR:只有三年大厂经验的不值20K

Several methods of importing excel file data by C #

微信小程序现金红包返回“IP地址非你在商户平台设置的可用IP地址”错误终极解决方法
随机推荐
What does the service grid that has been popular for two years bring to microservices? (Reprinted)
Introduction to vscade interface
Valarray Library Learning
Using SQL server agent job to restore the database regularly
使用阿里云免费的SSL证书
With a total data volume of more than trillions of lines, Yuxi cigarette factory can easily deal with it by correctly selecting the timing database
Verilog 每日一题(VL4 移位运算与乘法)
Linear algebra and matrix theory (10)
Connection design and test platform -- Summary of SystemVerilog interface knowledge points
零基础利用Unity3D开发AR应用并远程下载3D模型
利用SQL Server代理作业对数据库进行定时还原
Verilog 每日一题 (VL28 加减计数器)
Encrypt the video and upload it to OSS to achieve high concurrent access
Jupyter notebook win installation record
Goweb开发之Beego框架实战:第三节 程序执行流程分析
Zero foundation uses unity3d to develop AR applications and download 3D models remotely
mysql实现按照自定义(指定顺序)排序
Redis源码剖析,狠狠地拿捏了,赶紧码住
C语言实现扫雷小游戏
线性代数及矩阵论(八)