1

Tried searching around for a similar question, but was unable to solve this. Not sure how to properly convert some of the features in C++ loops. Especially the count < 20 condition in main().

Original question: https://rosettacode.org/wiki/Anti-primes

#include <iostream>

int countDivisors(int n) {
    if (n < 2) return 1;
    int count = 2; // 1 and n
    for (int i = 2; i <= n/2; ++i) {
        if (n%i == 0) ++count;
    }
    return count;
}

int main() {
    int maxDiv = 0, count = 0;
    std::cout << "The first 20 anti-primes are:" << std::endl;
    for (int n = 1; count < 20; ++n) {
        int d = countDivisors(n);
        if (d > maxDiv) {
            std::cout << n << " ";
            maxDiv = d;
            count++;
        }
    }
    std::cout << std::endl;
    return 0;
}

My attempted solution:

def countDivisors(n):
    if (n < 2):
        return 1
    count = 2

    for i in range(2, int(n/2)-1, 1):
        print(i)    
        if(n%i == 0):
            count = count + 1
    return count

def main():
    print("The first 20 anti-primes are: ")
    n = 1
    while count < 20:    
        d = countDivisors(n)
        if(d > maxDiv):
            print(n)
            maxDiv = d
            count += 1
            n += 1
    return 0

Required answer:

1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 
4
  • You reference maxDiv too early. What is initial value for maxDiv? Commented Dec 16, 2019 at 19:00
  • 1
    0. i actually initialized maxDiv and count to 0, but i removed it before i posted here. Commented Dec 16, 2019 at 19:04
  • Made a few edits to original question Commented Dec 16, 2019 at 19:15
  • Do your best to avoid thinking in C++ when writing Python code. Logic may be logic, but how best to implement logic in the two languages is very, very different. Commented Dec 16, 2019 at 19:33

4 Answers 4

1

Strictly speaking about the two for loops:

for (int i = 2; i <= n/2; ++i)

would become

for i in range(2,int(n/2)+1):

The int(n/2)-1 in your code would be i < n/2-1 in C.


The 20 one,

int maxDiv = 0, count = 0;    // <-- !
for (int n = 1; count < 20; ++n) {
    int d = countDivisors(n);
    if (d > maxDiv) {
        std::cout << n << " ";
        maxDiv = d;
        count++;
    }

is almost there, just you do not have maxDiv and count initialized in the Python one.

maxDiv = 0  # <-- !
count = 0   # <-- !
n = 1
while count < 20:    
    d = countDivisors(n)
    if(d > maxDiv):
        print(n)
        maxDiv = d
        count += 1
    n += 1
Sign up to request clarification or add additional context in comments.

3 Comments

Your translation of the second loop is wrong. The original one in C++ will loop more than 20 times, and break whencount, not i has reached 20.
@MartinBonnersupportsMonica true, I was focusing on the other loop.
@StormClaw: I do not want to edit it again, but the n += 1 part was an "autocorrect" without commenting: so in the question it was "too indented" and thus part of the if, not the while. Then it works, see ideone.com/48bWNu
0

You could try this. Does this yield the result you're looking for?

def countDivisors(n):
    if (n < 2):
        return 1
    count = 2

    for i in range(2, int(n/2)-1, 1):
        print(i)    
        if(n%i == 0):
            count = count + 1
    return count

def main():
        print("The first 20 anti-primes are: ")
        n,maxDiv = 1,0
        for d in range(1,20):    
            d = countDivisors(n)
            print(d)
            if(d > maxDiv):
                maxDiv = d
            n+=1
        return 0
if __name__ == "__main__":
    main()

2 Comments

tried it, didnt work out of the box. I'll take a closer look later to see if any small changes could possibly fix it.
added it in question, along with original question
0

Fundamentally the C for loop and the Python for loop are different. You have two choices.

The first choice is to do as you did, and use a Python while loop to replicate the test-and-stop part of the C for loop. If you do this, you have to write out the iteration yourself.

The second choice is to use a Python for loop to replicate the iteration, and write out the test-and-stop part of the code.

import itertools
.
.
.
        for n in itertools.count(1):    
            d = countDivisors(n)

            if(d > maxDiv):
                print(n)
                maxDiv = d
                count += 1
                if count >= 20:
                    break

I think I prefer this form, but it is very much a matter of taste.

1 Comment

I'm trying to avoid using libraries preferably hence why i'm using the while method. It's getting late here, so i'm gonna take another crack at this tomorrow. Mabye someone will come up with a solution by then.
0

I use a map function within countDivisors.

def countDivisors(n):
    if n < 2:
        return 1
    return 2 + sum(map(lambda x: 0 if n % x else 1, range(2, int(n / 2) + 1)))


def main():
    max_div = 0
    count = 0
    print("The first 20 anti-primes are:")
    x = 1
    while count < 20:
        d = countDivisors(x)
        if d > max_div:
            print(x, end=' ')
            max_div = d
            count += 1
        x += 1
    return 0


if __name__ == '__main__':
    main()

Try it online!

Output:

The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.