the Spartan way of C

In this forum members can discuss topics about specific programming languages.
Post Reply
lucasart
Posts: 4
Joined: Tue Aug 04, 2020 5:37 am

the Spartan way of C

Post by lucasart »

Working my way down these problems in pure C, without cheating. That means nothing more than the standard library.

No containers, dynamic arrays etc. Just realloc and free. This is Sparta!

No Bignum. Writing my own library from scratch, and learning so much along the way (Karatsuba multiplication and more :D).

Obviously, C is the worst choice possible (after assembly), and many problems are trivial in Python or other higher level languages with powerful libraries. But that's a feature. The way of pain teaches so much more.

PS: C and C++ are completely different ball games. They shouldn't be lumped together. It's obvious that this groupping was made by mathematicians, not programmers.
open_book
Posts: 3
Joined: Sun Jul 26, 2020 6:21 am

Re: the Spartan way of C

Post by open_book »

The way of pain teaches so much more.

Really? or are you just a masochist ?! :lol:
lucasart
Posts: 4
Joined: Tue Aug 04, 2020 5:37 am

Re: the Spartan way of C

Post by lucasart »

open_book wrote: Tue Aug 04, 2020 6:11 am The way of pain teaches so much more.

Really? or are you just a masochist ?! :lol:
Depends what you are looking for by doing project Euler. If you want to improve your programming skill, the way of pain teaches more. If you just want to tick boxes as fast as possible, then you should cheat as much as possible using libraries that do all the heavy lifting for you.

For me solving problems is not the most interesting part. The journey is more important than its destination.
open_book
Posts: 3
Joined: Sun Jul 26, 2020 6:21 am

Re: the Spartan way of C

Post by open_book »

Firstly, I would not characterize what you are advocating as the way of pain. OK, so if you are trying to improve your mathematics and programming skills by working on Project Euler. And, if you are insistent about not using third-party libraries, good for you! Learning to write Big Integer and other data-structures and algorithms by yourself is a laudable goal!

Here is a library you should try to implement yourself: https://hackage.haskell.org/package/arithmoi. This library was implemented by daniel.is.fischer who was a member of the Project Euler development team. You will see daniel's posts in the solution thread once you get into the 100 - 300 problem range. A while ago, I replicated a part of this library's functionality in Scala.

Since you claim to be using C, does that mean you will implement your own generic Set and Map abstractions? or will you use the C++ STL?

Anyway, this time around, I am taking an open-book approach to PE. The other day I wanted to solve a problem involving permutations, I needed a next_permutation function (this is available in C++ STL, I happen to be using a different language). I just googled and found an implementation of next_permutation on some website, and used it! In my earlier rounds on PE, I probably would have implemented next_permutation myself, but this time around, I do not wish to re-invent the wheel.

Very seriously consider learning the mathematics needed to implement arithmoi, that will really help you with PE problems!
pjt33
Posts: 51
Joined: Mon Oct 06, 2008 6:14 pm

Re: the Spartan way of C

Post by pjt33 »

lucasart wrote: Tue Aug 04, 2020 5:47 am PS: C and C++ are completely different ball games. They shouldn't be lumped together. It's obvious that this groupping was made by mathematicians, not programmers.
Bear in mind that the grouping was made two decades ago, when C++ had diverged much less from C than it has now.
lucasart
Posts: 4
Joined: Tue Aug 04, 2020 5:37 am

Re: the Spartan way of C

Post by lucasart »

open_book wrote: Tue Aug 04, 2020 9:45 am Firstly, I would not characterize what you are advocating as the way of pain. OK, so if you are trying to improve your mathematics and programming skills by working on Project Euler. And, if you are insistent about not using third-party libraries, good for you! Learning to write Big Integer and other data-structures and algorithms by yourself is a laudable goal!

Here is a library you should try to implement yourself: https://hackage.haskell.org/package/arithmoi. This library was implemented by daniel.is.fischer who was a member of the Project Euler development team. You will see daniel's posts in the solution thread once you get into the 100 - 300 problem range. A while ago, I replicated a part of this library's functionality in Scala.

Since you claim to be using C, does that mean you will implement your own generic Set and Map abstractions? or will you use the C++ STL?

Anyway, this time around, I am taking an open-book approach to PE. The other day I wanted to solve a problem involving permutations, I needed a next_permutation function (this is available in C++ STL, I happen to be using a different language). I just googled and found an implementation of next_permutation on some website, and used it! In my earlier rounds on PE, I probably would have implemented next_permutation myself, but this time around, I do not wish to re-invent the wheel.

Very seriously consider learning the mathematics needed to implement arithmoi, that will really help you with PE problems!
Thanks. But I think we are not at the same level mathematically. I'm coming to PE from a programming and problem solving angle. I just had a Eureka moment in discovering the Erathostene sieve. The arithmoi stuff is way too advanced mathematically for me.

Regarding STL, as I said pure C, which means only pointers, realloc and free. It's not that hard to to implement stuff like that.

At the moment I stopped solving PE and working on my BigInt library. Once ready I will use it to solve some simple ones (by brute force instead of mathematical analysts :lol: )
Post Reply