首页 > 代码库 > Codility---CountFactors
Codility---CountFactors
Task description A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M. For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4). Write a function:
that, given a positive integer N, returns the number of its factors. For example, given N = 24, the function should return 8, because 24 has 8 factors, namely 1, 2, 3, 4, 6, 8, 12, 24. There are no other factors of 24. Assume that:
Complexity:
|
Solution
Programming language used: Java
Code: 15:02:17 UTC, java, final, score: 100
show code in pop-up
12345678910111213141516171819202122
// you can also use imports, for example:// import java.util.*;// you can write to stdout for debugging purposes, e.g.// System.out.println("this is a debug message");class Solution { public int solution(int N) { // write your code in Java SE 8 int count = 0, i=1; double max = Math.sqrt(N); for(; i < max; i++) { if(N % i ==0) { count += 2; } } if(i*i == N){ count++; } return count; }}
https://codility.com/demo/results/training5YTT4B-5NP/
Codility---CountFactors
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。