Project Euler / programming problems

i must code in C at 42 school, not C++, do your tool you share here work for C ?

next problem?

1 Like

I remember playing this one too. It was a cool concept, even though normally I’m not a fan of programming games.
Among programming games I heard very positive reviews of EXAPUNKS where you have this map with some robots on it and you need to write algorithms in ASM(esque) to move and loop them around so that they reach all the objectives on the map.

Um… maybe it’s just me, but the vast majority of “programming games” on steam are just puzzle games masked behind a haxxor-ui. That’s not how programming is to me.

Programming is problem-solving, sure. But the main attraction in it is freedom and creativity in solution.

  • The first thing is you build thing, and not break thing. Breaking is part of a hacker’s job, programmers are primarily builders. Puzzle games are “Breaking” type of creativity, not “Building” (you look for the “vital point” of the problem, and once you see that it’s “solved” for you)
  • The second thing is freedom. The challenge you should overcome should be speed and intelligence, and you should be free to do whatever you want using whatever tool you need to reach the goal. Solving the problem using a limited number of character and consume a limited number of memory is academical problems, and ironically academical problems have very little use in daily practice.

This is the reason why I occasionally do Project Euler’s problems, but don’t even want to touch the 3 games @yebellz showed above, imo those 3 are all haxxor games that have very little benefit for my improvement as a programmer :stuck_out_tongue:

Number 7 I believe

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

1 Like
Ok thought my solution was dumb but it worked
const findSweatNumber = (target) => {
  let primes = [2, 3];

  // primes are always 6a(+-)1
  const variants = [-1, 1];
  let sixIndex = 1;

  while (primes.length < target) {
    for (let i of variants) {
      const number = 6 * sixIndex + i;
      if (isPrime(number, primes)) {
        primes[primes.length] = number;
      }
    }
    sixIndex++;
  }
  return primes[target - 1];
};

const isPrime = (number, previousPrimes) => {
  const limit = Math.sqrt(number);

  for (let prime of previousPrimes) {
    if (prime > limit) break;
    if (number % prime == 0) return false;
  }

  return true;
};

console.log(findSweatNumber(process.argv[2]));

1 Like

RangeError: Maximum call stack size exceeded

No fun allowed.

unfun solution

Avoided an implicit boolean-integer casting as a way to have fun even though it was completely unnecessary as I can just start with 2 instead of 1.

const isPrime = (number) => {
    for (let i = Math.floor(Math.sqrt(number)); i > 1; i--) {
        if (number % i == 0) return 0;
    }

    return 1 - Math.floor(number ** -1);
}

const getNthPrime = (order) => {
    let count = 0;
    let i = 2;
    
    while (count < order) {
        count += isPrime(i);
        i += 1;
    }

    return i - 1;
}

console.log(getNthPrime(10001));

@esoka

What does this mean? I don't get it o_0

I think it’s a funny way to say:

return number === 1 ? 0 : 1;
1 Like

But the number start from 2 :sweat_smile:

1 Like

I think that’s what the comment was referring to “completely unnecessary as I can just start with 2 instead of 1”

1 Like
Summary

It’s Math.floor(Math.pow(number, -1)) in which Math.pow(number, -1) returns a sub-zero value for everything except for when it’s 1, so flooring it returns 1 if it equals to 1 and 0 if it’s more than 1. When subtracted from 1 it turns it into 0 if the number equals 1.

So effectively it’s return number && !(number == 1) or as benjito said return number == 1 ? 0 : 1; where all of these zeroes and ones are actually true and false but I wanted them in numeric form to be able to do count += isPrime(i) without an implicit conversion from boolean to number or an if-statement.

Without checking I’d assume that Math.floor is slower than an if, so all of this is way slower than doing it normally. I just did it for an additional challenge.

2 Likes

Math.pow might be even slower than floor haha (I have not benchmarked, just seems like a more complicated operation to me)

Edit: someone did benchmark and seems to confirm, pow about 2.5x slower: Benchmark of Elementary Mathematical Operations in Node.js – Akseli Palén

That’s actually surprising but I haven’t thought of it enough before. I assume floor is something like return ((number * 10) - (number % 10)) / 10 and pow is return Math.exp(power * Math.log(number)). I guess when you look at it this way it’s obvious that pow is slower because of the log but if it was an interview question I probably wouldn’t have answered it correctly.

Edit: never mind floor isn’t that simple. Maybe there’s some cool maths to do it to avoid a loop.

All I know is that I can calculate floor without a calculator, exponents/logarithms not so much :joy:

I can calculate ceil and round without any aid too. Maybe I should’ve done a maths degree.

Pseudocode for how a flooring algorithm might work:

image

  • copy the fraction bits and copy them into an int
  • prepend them with 1 (IEEE doesn’t store the 1 explicitly, since every number starts with 1)
  • bitshift left by the exponent minus 23

Notes:

  • This is a float-32 algorithm, but not too hard to extend it to float-64
  • No division (division is slightly slower than multiplication and bitshifting)
  • There are of course machine ops for all of these, so the “true” implementation probably isn’t very decipherable at all
  • No need to use base-10 arithmetic since floor works the same in all radixes
1 Like

Wow it sucks how you’re being happy just for getting the correct answer, and then look at their forum to see people’s algorithms solving the same thing in nanoseconds…

1 Like

To be fair, just talking about the floor function in the last comment, not the actual prime-finding. Outside of switching to a faster language, I’m not sure I could get much more performant than your solution

No I meant the Euler’s forum :sweat_smile:

1 Like