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:

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

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/
时间: 2024-10-11 18:14:39

Codility---CountFactors的相关文章

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

the solution of CountNonDivisible by Codility

question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn); Then  we iterate array A to get the ith non-di

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

codility上的问题(34) Fluorum 2014

好久没写codility的题了,一来没时间,二来有的题目不太好分析.这个题比较有意思,我还没有给出非常严格的证明. 给定一棵树(无向无环图),从一个节点出发,每次选择一个节点,从起点到目的节点的路径上没经过的节点尽可能多,直到遍历完所有的节点.如果起点到两个目的节点的路径中没经过的节点同样多,则选择标号较小的节点作为目的节点.如此继续,直到遍历所有的节点,求按顺序选择了哪些目的节点? 例如从2 开始,第一次选择0,然后1,0变为经历过的节点. 然后从0开始,第二次选择6, 然后4,6变为经历过的

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

*[codility]Country network

https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave

Codility上的问题(35) Neon 2014

也是比较有意思的题,越来越数学了--不善于做这种题. 如图一个码头有N个木桩,用于拴住船,码头长度是M,可以理解未0到M的线段.有N调船,每条船的一半长度为X,所以船长是2 * X.每个船的中心必须拴在一个木桩上.并且每个木桩只能拴一条船.拴船的绳子长度是船的中心与木桩位置的距离.当然,木桩的位置不能移动,但是船可以自由左右移动.要求船头船尾必须都在码头上(0..M的线段),船也可以看作长度为2 * X的线段,请给每条船指定一个位置让拴船的最长绳子长度最短,求这个最短的绳子长度.如果无法容纳下所

[codility] Lession1 - Iterations - BinaryGap

Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of