1
$\begingroup$

I've been doing Project-Euler just as a way to increase my competency in computer science. I'm currently a Pure and Applied Math major who recently adopted computer science as a minor in order to apply to grad schools in comp. sci.

Some built-in Sage functions that are extremely fast and I do not have comparably fast definitions include gcd, lcm, .intersection(), .is_prime(), prime_range(), .factor(), solve(),...

Many Project-Euler problems I've been able to solve relatively easily and quickly with these built-in functions but when I see the forums after answering the question, other people's answers are quite lengthy (as they are writing they're own code).

I'm debating if my method of getting through Project-Euler is truly a learning environment. I'm definitely learning more Sage commands and getting better at using Sage but I'm not sure my coding is getting any better unless I make my own definitions.

In essence what is the transition period between using built-in definitions to strictly using your own independently created code? Or what's more beneficial, being able to create your own relatively fast code or getting the job done as fast as possible (using built-in methods)?

$\endgroup$

2 Answers 2

1
$\begingroup$

It depends upon what your goal is for doing Project Euler. Many of the later problems need creative problem solving skills rather than brute force for their approach so for me I use it as a means to involve my problem solving and algorithm design skills rather than my programming skills in a specific language. If you want to do both make sure you could write each of the functions you are using and then test and compare your versions to the built in ones. Yours will probably not be as fast but they should have a similar big O notation for their speed.

$\endgroup$
0
$\begingroup$

From the perspective of a programmer/coder, it isn't a bad practice to use inbuilt functions when you can (in fact, people are lazy, if there's an inbuilt function, why do the work of manually defining another?)

Now, from the perspective of a mathematician, the aim of the problems there is to motivate you to understand algorithms properly (i.e., understand how it works, how the algorithm is constructed from basic tools) and then implementing them on your own to solve those problems.

Take your $\gcd()$ function for example. The mathematician's way to solve this would be implementing the Euclidean algorithm (recursive definition to find gcd). There's a one-line definition for it :

int gcd(int a,int b){
    return (b==0) ? a : gcd(b,a%b);
}

For lcm, you can just use the fact that $\textrm{lcm}(a,b)\gcd(a,b)=ab$ for positive integers $a,b$.

And similarly, you can think up of algorithms for all the other functions. After all, those inbuilt functions were programmed from scratch by someone using these basic mathematical algorithms.

So, in conclusion, there's no good or bad side to this, it just depends on your perspective. But since you're majoring in Mathematics and not Comp Sci, I'd recommend using inbuilt functions as rare as possible (use only when you can't think of an elementary algorithm to do it). If you do use an inbuilt function, try to search for a mathematical algorithm afterwards if you can and understand it well, so as to gain experience.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .