#How to fix this error prime

4 messages · Page 1 of 1 (latest)

valid dragon
#

public static int kthPrime(int k) {
if (k == 0) {
return 2;
}
int counter=0;
for (int i=0;i<=Integer.MAX_VALUE;i++){
if (isPrime(i)){
counter++;
if(counter==k)
return i;
}
}
return -1;

        }
public static List<Integer> factorize(int n) {
    List<Integer> factorize = new ArrayList<>();

    if (n== 0)
        return factorize;

    factorize.add(1);

    if (n == 1)
        return factorize;

    if (n < 0)
        n*= -1;
    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
            factorize.add(i);
            if (i != (n/ i))
                factorize.add(n / i);
        }
    }
    factorize.add(n);
    factorize.sort(null);

    return factorize;
}

// test
@Test public void factorizeTest() {
CRC32 check = new CRC32();
for(int k = 2; k < 500_000; k++) {
List<Integer> factors = Primes.factorize(k);
int prod = 1;
int prev = 0;
for(int f: factors) {
assertTrue(f >= prev);
prev = f;
prod = prod * f;
check.update(f);
}
assertEquals(k, prod);
}
assertEquals(2607517043L, check.getValue());

}

}

//expected
Expected :4
Actual :8

vague coveBOT
#

This post has been reserved for your question.

Hey @valid dragon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#
public static int kthPrime(int k) {
        if (k == 0) {
            return 2;
        }
        int counter=0;
        for (int i=0;i<=Integer.MAX_VALUE;i++){
            if (isPrime(i)){
                counter++;
                if(counter==k)
                    return i;
            }
        }
        return -1;

            }
    public static List<Integer> factorize(int n) {
        List<Integer> factorize = new ArrayList<>();

        if (n== 0)
            return factorize;

        factorize.add(1);

        if (n == 1)
            return factorize;

        if (n < 0)
            n*= -1;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                factorize.add(i);
                if (i != (n/ i))
                    factorize.add(n / i);
            }
        }
        factorize.add(n);
        factorize.sort(null);

        return factorize;
    }



//  test
@Test public void factorizeTest() {
        CRC32 check = new CRC32();
        for(int k = 2; k < 500_000; k++) {
            List<Integer> factors = Primes.factorize(k);
            int prod = 1;
            int prev = 0;
            for(int f: factors) {
                assertTrue(f >= prev);
                prev = f;
                prod = prod * f;
                check.update(f);
            }
            assertEquals(k, prod);
        }
        assertEquals(2607517043L, check.getValue());

    }
}



//expected
Expected :4
Actual   :8