#C++ Compiler (in-browser)

33 messages · Page 1 of 1 (latest)

grave furnace
#
#include "prime_factors.h"

#include <iostream>

#include <iostream>
using namespace std;

namespace prime_factors {
    bool is_prime( int n ) {
        for (auto i = 2 ; i <= n/2 ; i++ )
            if (n % i == 0) return false;
        return true;
    }


    vector<int> primes(int n) {
        vector<int> res;

        for (auto i = 2; i <= n ; i++)
            if (is_prime(i))  {
                res.push_back(i);
            }
                

        return res;
    }

void display( vector<int> v) {

    cout << "vector<int>{";
    
    for (auto it = v.begin() ; (it != v.end()) ; it++ )
            if (it == v.begin())
                cout << *it ;
            else
                cout << ", " << *it;

            cout << "}" << endl;
}

    vector<int> of(int n) {
        vector<int> res, t = primes(n);
        int div; 

        if (n < 2) return {};
        if (is_prime(n)) return {n};

        for (auto it = t.begin() ; (n > 1) && (it != t.end()) ; it++ )
            {
                div = *it;

                while ( n % div == 0)
                    {
                        res.push_back(div);
                        n /= div;
                    }
            }
       
        return res;
        
    }
};  // namespace prime_factors

This c++ code is not funtionning under the in-browser compiler, but does function locally

And here's the header file :

#if !defined(PRIME_FACTORS_H)
#define PRIME_FACTORS_H

#include <vector>

using namespace std;

namespace prime_factors {
    vector<int> of(int);

}  // namespace prime_factors

#endif // PRIME_FACTORS_H
peak jay
#

What is the error you are getting? Is it a compiler error, or is a test not passing?

grave furnace
#

It is a compiler error, but currently not on my computer, so I cannot copy it here by now

umbral fern
#

Did you enable and run all the tests on your computer?
Did you get a timeout?
of() calls primes(n), primes(n) loops over all numbers from 2 to n and calls is_prime(i), and this is_prime(i) loops over all numbers from 2 to n/2and performs a modulo operation to check if i is divisible by some factor.
That's a lot of modulo operations, especially if you consider that one of the tests calls of(901255). The runtime complexity is in O(n²), on my computer the 10 test case take about 1 minute to run.
You will need a faster solution.

#

Or do you get a compiler error like this one:

/tmp/prime-factors/prime_factors.cpp: In function 'void prime_factors::display(std::vector<int>)':
/tmp/prime-factors/prime_factors.cpp:32:5: error: this 'for' clause does not guard... [-Werror=misleading-indentation]
   32 |     for (auto it = v.begin() ; (it != v.end()) ; it++ )
      |     ^~~
/tmp/prime-factors/prime_factors.cpp:38:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   38 |             cout << "}" << endl;
      |             ^~~~
cc1plus: all warnings being treated as errors

Take a look at the for loop in display(). The compiler is warning you that the final cout << "}" << endl; is at the same indentation level as the if/else but since the for loop has no curly braces this cout line is not part of the loop.

grave furnace
#

yes , this is a compiler error,

We received the following error when we ran your code:
/tmp/prime-factors/prime_factors.cpp: In function 'void prime_factors::display(std::vector<int>)':
/tmp/prime-factors/prime_factors.cpp:27:5: error: this 'for' clause does not guard... [-Werror=misleading-indentation]
   27 |     for (auto it = v.begin() ; (it != v.end()) ; it++ )
      |     ^~~
/tmp/prime-factors/prime_factors.cpp:33:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   33 |             cout << "}" << endl;
      |             ^~~~
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/prime-factors.dir/build.make:90: CMakeFiles/prime-factors.dir/prime_factors.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/prime-factors.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
crimson girder
#

Ah. Yeah. That seems to pretty cleanly indicate it's complaining about indentation

grave furnace
crimson girder
#

That may have to do with -W flags

#

What "issues" are considered errors and stop the compiler

grave furnace
#

where could there be indentation rules in c++, lol

crimson girder
#

In the compiler.

grave furnace
#

the language doesn't tell indentation rules ?

crimson girder
#

That's why it is (I think) a warning and not an error

umbral fern
#

You get the exact same error message on the compiler explorer if you use the compiler options from the CMakeLists.txt

grave furnace
#

Yes, you're right on this compiler error/warning, which I don't understand, but anyway it was a display function , useless for the solution, and if I comment all this display function because it is unused, here(s what I get :

YOUR TESTS TIMED OUT
Your tests timed out. This might mean that there was an issue in our infrastructure, but more likely it suggests that your code is running slowly. Is there an infinite loop or something similar?

Please check your code, and if nothing seems to be wrong, try running the tests again.
umbral fern
#

Did you read my first comment? This solution is very slow, too slow for the test runner on Exercism's servers. You will need something faster.

grave furnace
#

Yes, I have read your comment, and No it is not slow; because it is not loops inside loops, it is sequentiel loops, and it is functionning/compiling on "compiler explorer" within your environment of execution as you sent me

#

We cam optimize by memorizing between loops, and also divide number of loops,

umbral fern
#

On your computer, how long does it take to execute all 10 test cases?

grave furnace
#

here it is

hadoop@GTR6:~/tmp$ ./repeat 10 ./ess
10: vector<int>{2, 3}
9: vector<int>{2, 3}
8: vector<int>{2, 3}
7: vector<int>{2, 3}
6: vector<int>{2, 3}
5: vector<int>{2, 3}
4: vector<int>{2, 3}
3: vector<int>{2, 3}
2: vector<int>{2, 3}
1: vector<int>{2, 3}
hadoop@GTR6:~/tmp$ times ./repeat 10 ./ess
0m0.017s 0m0.024s
0m0.058s 0m0.050s
hadoop@GTR6:~/tmp$ 
umbral fern
#

are those the 10 tests cases from exercism, including the last one which calls of(901255)?

grave furnace
#

adoop@GTR6:~/tmp$ cat ess.C
#include <vector>

using namespace std;

#include <iostream>
using namespace std;

namespace prime_factors {
    bool is_prime( int n ) {
        for (auto i = 2 ; i <= n/2 ; i++ )
            if (n % i == 0) return false;
        return true;
    }


    vector<int> primes(int n) {
        vector<int> res;

        for (auto i = 2; i <= n ; i++)
            if (is_prime(i))  {
                res.push_back(i);
            }
                

        return res;
    }

void display( vector<int> v) {

    cout << "vector<int>{";
    
    for (auto it = v.begin() ; (it != v.end()) ; it++ )
            if (it == v.begin())
                cout << *it ;
            else
                cout << ", " << *it;

            cout << "}" << endl;
}

    vector<int> of(int n) {
        vector<int> res, t = primes(n);
        int div; 

        if (n < 2) return {};
        if (is_prime(n)) return {n};

        for (auto it = t.begin() ; (n > 1) && (it != t.end()) ; it++ )
            {
                div = *it;

                while ( n % div == 0)
                    {
                        res.push_back(div);
                        n /= div;
                    }
            }
       
        return res;
        
    }
};  // namespace prime_factors

int main()
{
    vector<int> v = prime_factors::of(6);

    prime_factors::display( v );
}
hadoop@GTR6:~/tmp$ 
umbral fern
#

so those are NOT the 10 test cases from Exercism. Then you cannot compare the runtime to the runtime of the tests.

#

I'd suggest using the CLI to download the exercise. That would allow you to compile/execute all the official tests. And you wouldn't have to check "visually" whether a solution is correct.
But if that's not an option then here are the arguments from the 10 tests: 1, 2, 3, 4, 6, 8, 9, 27, 625, 901255

grave furnace
#

Yes, you're right, it's very long for n=901255, i have to change some things, lol

crimson girder
#

@umbral fern does make it a habit of being right 😉

grave furnace
#

thank you for your patience @umbral fern @crimson girder

#

OK, I optimized a little bit (I didnt think at it first)

hadoop@GTR6:~/tmp$ times ./repeat 10 ./ess
0m0.022s 0m0.054s
0m21.590s 0m1.022s
#

here's my optimization :

   bool is_prime( int n ) {
        for (auto i = 3 ; i <= sqrt(n) ; i += 2 )
            if (n % i == 0) return false;
        return true;
    }
umbral fern
#

I'm not sure what your most recent benchmark is measuring. Did you run the the 10 tests or one test 10 times?

grave furnace
#

ess.C :

int main()
{
    vector<int> v;
    v = prime_factors::of(2);
    prime_factors::display(v);

    v = prime_factors::of(3);
    prime_factors::display(v);

    v = prime_factors::of(4);
    prime_factors::display(v);

    v = prime_factors::of(6);
    prime_factors::display(v);

    v = prime_factors::of(8);
    prime_factors::display(v);

    v = prime_factors::of(9);
    prime_factors::display(v);

    v = prime_factors::of(27);
    prime_factors::display(v);

    v = prime_factors::of(625);
    prime_factors::display(v);

    v = prime_factors::of(901255);
    prime_factors::display(v);

}
                                                                                           
#

but going to work at it more