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. vultr的80端口?

    1.查看防火墙版本号firewall-cmd --version2.查看防火墙状态firewall-cmd --state3.添加80端口的权限firewall-cmd --zone=public - ...

  2. html head标签的内容跑到body标签中 , 并且body中多了个空格

    今天遇到一个奇怪的问题 , 就是在head标签中写的内容跑到body标签中 , 第一种也是经常遇到的情况就是编码 UTF-8 格式带BOM的 , 这种情况是会多一个空格 , 这个基本都知道 , 按ut ...

  3. 转载来自朱小厮博客的 一文看懂Kafka消息格式的演变

    转载来自朱小厮博客的 一文看懂Kafka消息格式的演变     ✎摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在 ...

  4. 三星260亿美元的豪赌:想垄断DRAM和NAND闪存市场(规模经济让对手难以招架)

    腾讯科技讯 据外媒报道,经过50年的发展,半导体市场仍然显得非常活跃,它在今年有望增长20%.随着高增长而来的是供应短缺,这就是DRAM和闪存价格为什么今年会上涨的原因. 三星在DRAM和闪存市场占有 ...

  5. 2.RabbitMQ的quick start

    1.首先要安装 RabbitMQ Client 2.创建一个控制台项目 取名 Publisher代码如下: using RabbitMQ.Client; using System; using Sys ...

  6. framework7使用问题汇总

    framework7 是个非常漂亮的Html框架,最近有个微信公众号的项目使用到了这个,后期还可以封装成APP. 淘宝版和中文官网都是V1,V2只能看英文版的http://framework7.io/ ...

  7. Viewport3D中的摄像机(二、摄像机动作)

    原文:Viewport3D中的摄像机(二.摄像机动作) 前文介绍了Viewport3D中的两种摄像机:OrthographicCamera和PerspectiveCamera.在3D场景里漫游,最主要 ...

  8. Android到您的计算机使用命令行屏幕捕获和出口

    声明:本博客为原创博客,未经同意.不得转载! 原文链接为http://blog.csdn.net/bettarwang/article/details/27819525 大多数人最经常使用的截屏方法可 ...

  9. C. Adidas vs Adivon

    C. Adidas vs Adivon Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer ...

  10. QT如何编译出带调试信息的qtwebkit库

    因为在编译QT的时候默认是不编译成带调试信息的qtwebkit库的,不论如何设置参数都是没有用的.后面在一博客中查找到相关信息   1.编译带debug 信息的webkit 库 注释或者删除qt-ev ...