Integer's divisors 关于整数的约数

Create a function named divisors/Divisors that takes an integer and returns an array with all of the integer‘s divisors(except for 1 and the number itself). If the number is prime return the string ‘(integer) is prime‘ (null in C#) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust).

Example:

divisors(12); // should return [2,3,4,6]
divisors(25); // should return [5]
divisors(13); // should return "13 is prime"

方法一
 1 function divisors(integer) {
 2   var arr = [];
 3   for(var i = 2; i < integer; i++){
 4      if(integer % i ===0){
 5      arr.push(i);}
 6    }
 7   if(arr.length ===0){
 8   return integer+‘ is prime‘;
 9   }else{
10   return arr;}
11 };
上面方法不够完美,for循环里面 i <integer ,有重复,可以用如下方法,更简洁方法二
1 function divisors(integer) {
2   var arr = [];
3   for (var i = 2; i <= Math.floor(integer / 2); ++i)
4   if (integer % i === 0) arr.push(i);
5   return arr.length ? arr : integer + ‘ is prime‘;
6 };
 

Integer's divisors 关于整数的约数

时间: 2024-10-10 18:23:02

Integer's divisors 关于整数的约数的相关文章

Leetcode#13. Roman to Integer(罗马数字转整数)

题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的。

/* 有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的.例如: 9的约数和有:1+3=4 4的约数和有:1+2=3 所以9和4不是友好的. 220的约数和有:1  2  4  5  10  11  20  22  44  55  110=284 284的约数和有:1  2  4  71  142=220 所以220和284是友好的. 编写程序,判断两个数是否是友好数. */ #include <stdio.h> #include<string.h>

poj2992 divisors 求组合数的约数个数,阶乘的质因数分解

Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you need any special reason for such a useful computation? Input The input consists of several instances. Each instance consists of a single line containin

[LeetCode]41. String to Integer(atoi)字符串转整数

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

[LeetCode] String to Integer (atoi) 字符串转为整数

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

leetcode - String to Integer (atoi) 字符串转整数

Implement atoi to convert a string to an integer. Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. 题目:实现字符串转整数 注意事项:考虑好各种可能的输入(坑); public class Solution

Leetcode8---&gt;String to Integer(实现字符串到整数的转换)

题目: 实现字符串到整数的转换 解题思路: 下面给出这道题应该注意的一些细节: 1. 字符串“    123   ” = 123: 2.   字符串“+123” = 123: 3.   字符串“-123” = -123: 4.   字符串“-” = 0:“+” = 0: 5.   字符串“123-” = 123: 6.   字符串“21474836478” = 2147483647(Integer.MAX_VALUE) 7.   其余情况都是非法输入,一律返回0: 代码如下: 1 public

[LeetCode]47. Integer to English Words整数的读法

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g

[Leetcode] Roman to integer 罗马数字转成整数

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路:有关罗马数字的相关知识可见博客Integer to roman.从左往右遍历字符串,比较当前为和下一位的值,若是当前数字比下一位大,或者当当前数字为最后时,则加上当前数字,否则减去当前数字.这就遇到一个问题,罗马数字的字符串不是按26个字母顺序来的,如何确定大小.解决办法一:写