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:

class Solution { public int solution(int N); }

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:

  • N is an integer within the range [1..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(sqrt(N));
  • expected worst-case space complexity is O(1).
Solution

 
Programming language used: Java
Code: 15:02:17 UTC, java, final, score:  100
// 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的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  10. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. ssh远程连接docker中的 linux container

    ssh远程连接docker中的container   由于工作需要,要远程连接Container,本地机器是windows,以下为解决步骤: 1. 环境 本地:Windows ↓ Docker版本1. ...

  2. C#6

    C#6   1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: 1 public string FirstName { get; privat ...

  3. Valid page threshold based garbage collection for solid state drive

    A method for garbage collection in a solid state drive (SSD) includes determining whether the SSD is ...

  4. ssm框架插入mysql数据库中文乱码问题解决

    1.      检查web.xml <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-n ...

  5. HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合

    Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  6. Linux性能测试 KSysguard工具

    KDE System Guard (KSysguard)是KDE的任务管理和性能监控工具.它采用client/server架构,可以监控本机也可以监控远端主机. KDE System Guard默认的 ...

  7. 嵌入式OS入门注意事项-同RTX案件:九.关于优先-翻转,继承和天花板

    嵌入式OS入门注意事项-同RTX案件:九.关于优先-翻转,继承和天花板 涉及当调度优先级,会有很多问题,本文中,优先调度和一些战略的主要问题,以应付. 有几个概念如下:(priority invers ...

  8. crossplaform---Nodejs in Visual Studio Code 04.Swig模版

    1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...

  9. C#同步SQL Server数据库Schema

    C#同步SQL Server数据库Schema 1. 先写一个sql加工类: using System; using System.Collections.Generic; using System. ...

  10. 基于Linux C的socketEthereal程序和Package分析 (一个)

     执行测试平台:CentOS 6.5发行版,内核版本号3.11 1. Linux抓包源程序 在OSI七层模型中.网卡工作在物理层和数据链路层的MAC子层. 进行网络通信时.源主机通过socket( ...