fix https links
This commit is contained in:
@@ -1 +1 @@
|
||||
Finally a opportunity to use my favorite algorithm: [The sieve of Eratosthenes.](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
|
||||
Finally a opportunity to use my favorite algorithm: [The sieve of Eratosthenes.](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
|
||||
@@ -1,2 +1,2 @@
|
||||
The main thing here was sorting the list. I used [bubble sort](http://en.wikipedia.org/wiki/Bubble_sort), which is not the fastest algorithm but was pretty easy to implement in befunge.
|
||||
The main thing here was sorting the list. I used [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort), which is not the fastest algorithm but was pretty easy to implement in befunge.
|
||||
The rest is just calculating the single scores.
|
||||
@@ -1,4 +1,4 @@
|
||||
If you looked at the previous problems you probably know what comes now ... (Sieve of Eratosthenes)[http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes].
|
||||
If you looked at the previous problems you probably know what comes now ... (Sieve of Eratosthenes)[https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes].
|
||||
To lower the amount of A-B combinations we have to check here are 2 rules I found out:
|
||||
|
||||
- `B` must be a (positive) prime, otherwise n=0 wouldn't yield a prime number
|
||||
|
||||
@@ -2,5 +2,5 @@ This one was relaxing. You can simple iterate through all 8100 cases and test ea
|
||||
Three notable things:
|
||||
|
||||
- You can test two fraction `a/b` and `c/d` simple of equality with the formula `a*d == b*c`.
|
||||
- This is my compact [GCD algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) in befunge (stack -> stack)
|
||||
- This is my compact [GCD algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) in befunge (stack -> stack)
|
||||
- This is one of the few *real* Befunge-93 programs in this series. (It doesn't violate the [80x25](https://github.com/catseye/Befunge-93/blob/master/doc/Befunge-93.markdown) size rule)
|
||||
@@ -1,2 +1,2 @@
|
||||
Thats one big [sieve](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), even though not as big as the on in problem 10.
|
||||
Thats one big [sieve](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), even though not as big as the on in problem 10.
|
||||
Here we needed a way to "rotate" a number: `value = value%10 * 10^digitcount + value/10`. The rest is standard befunge stuff and trying to optimize it.
|
||||
@@ -1,8 +1,8 @@
|
||||
My first attempt at this problem took over 5 hours to compute and had a complexity of O(n^3).
|
||||
|
||||
The problem is that you need a square root to inverse the pentagonal formula and Befunge has no square root function.
|
||||
So I needed to implement my own version of integer square roots in Befunge (see [wikipedia](http://en.wikipedia.org/wiki/Methods_of_computing_square_roots)).
|
||||
So I needed to implement my own version of integer square roots in Befunge (see [wikipedia](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots)).
|
||||
The program is still not really fast but it's good that I managed to speed it up to a time where you can execute it without waiting the whole night.
|
||||
|
||||
Also this program is nicely compact, by the time I'm writing this my Befunge interpreter [BefunExec](http://www.mikescher.de/programs/view/BefunGen) has gotten a display of all possible paths a program can take.
|
||||
Also this program is nicely compact, by the time I'm writing this my Befunge interpreter [BefunExec](https://www.mikescher.de/programs/view/BefunGen) has gotten a display of all possible paths a program can take.
|
||||
And if you look at the graph of this program, it looks pretty interesting...
|
||||
@@ -1,4 +1,4 @@
|
||||
I really missed my [sieve of erastothenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). There were really a few problems without primes in a row.
|
||||
I really missed my [sieve of erastothenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). There were really a few problems without primes in a row.
|
||||
|
||||
In this problem we go through all primes `i`, search through all smaller primes `j` were `(i-j)/2` is a quadratic number. If you can't find one, this falsifies the theorem.
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@ This is only an approximation, but a rather good one. In tested the numbers from
|
||||
|
||||
So now we've got the numbers where `digit_product(p) == digit_product(p+3330) == digit_product(p+6660)` (in fact there are only 40 of these).
|
||||
We then use a simple primality test to check if all three numbers are prime.
|
||||
The primality test is basically a port of the [wikipedia](http://en.wikipedia.org/wiki/Primality_test) version to befunge. Wit it we only have to do `n/6` divisions to test a number `n` for primality.
|
||||
The primality test is basically a port of the [wikipedia](https://en.wikipedia.org/wiki/Primality_test) version to befunge. Wit it we only have to do `n/6` divisions to test a number `n` for primality.
|
||||
|
||||
All in all tis leads to a fast, small and not very grid-intensive program (Only one field is used, *and only as a temporary storage to swap three values*).
|
||||
@@ -2,7 +2,7 @@ This is effectively an optimized implementation of [this algorithm](http://www.m
|
||||
You can see the ten patterns on the left side and beside them the area were we build our numbers.
|
||||
|
||||
So what we do is iterate through the numbers from `100` to `1 000`, through the ten patterns and through the digits `0`, `1` and `2`.
|
||||
In each iteration we generate the number (from value, pattern and digit) and test for it primeness (with a simple [primality test](http://en.wikipedia.org/wiki/Primality_test) - no prime sieve).
|
||||
In each iteration we generate the number (from value, pattern and digit) and test for it primeness (with a simple [primality test](https://en.wikipedia.org/wiki/Primality_test) - no prime sieve).
|
||||
If we found a prime we count the number of primes in it's family and if this count is eight we print the generated number and exit.
|
||||
|
||||
This program is not the fastest, because I check all the primes "manually" and not with an prime sieve each iteration takes quite a time.
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
*That* is the kind of problem I really like. There is a lot of depth in the solution and a proper algorithm solves this in no time.
|
||||
|
||||
The algorithm idea here is that we use the properties of [Pascal's Triangle](http://en.wikipedia.org/wiki/Pascal%27s_triangle).
|
||||
The algorithm idea here is that we use the properties of [Pascal's Triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle).
|
||||
As you probably know this triangle starts at its top with a `1`. Then every cell is calculated as the sum of the two cells above it.
|
||||
|
||||
~~~
|
||||
cell[y][x] = cell[y-1][x-1] + cell[y-1][x]
|
||||
~~~
|
||||
|
||||

|
||||

|
||||
|
||||
When we calculate `C(n, r)` this is nothing more than the number at row *n* and column *r*.
|
||||
|
||||
So what we do is build [Pascal's Triangle](http://en.wikipedia.org/wiki/Pascal%27s_triangle) up to a height of one-hundred and look at the cells with values greater than one million.
|
||||
So what we do is build [Pascal's Triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle) up to a height of one-hundred and look at the cells with values greater than one million.
|
||||
The obvious problem is that the numbers grow extremely big, and sooner or later *(probably sooner)* the numbers will overflow. So what we do is use a little trick:
|
||||
As soon as a cell is over `1 000 000` we mark her *(= put a zero in that cell)*. When we create a new cell out of its two parents we check if one of the parents has the mark *(= is zero)* and then the children gets also marked. Because if one of the parents (or both) is over one million then all of its children will also be over one million.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
It's obvious that the bottleneck of this program is the primality test.
|
||||
The numbers become here too big to create a sieve and "normal" prime testing takes too long.
|
||||
So we use the [Miller-Rabin primality test](http://en.wikipedia.org/wiki/Miller-Rabin_primality_test) that I implemented a while ago (thank [mathblog.dk](http://http://www.mathblog.dk)).
|
||||
So we use the [Miller-Rabin primality test](https://en.wikipedia.org/wiki/Miller-Rabin_primality_test) that I implemented a while ago (thank [mathblog.dk](https://http://www.mathblog.dk)).
|
||||
The rest is just enumerating all the diagonals until `primes*10<all`
|
||||
@@ -7,7 +7,7 @@ We generate primes from `1` to `3300` and save verified pairs in an Hashmap.
|
||||
And when I say Hashmap I mean an *fucking* `3000x3000` array where every possible pair has an field (yay for befunge).
|
||||
|
||||
I had to use quite a few codesnippets from older project:
|
||||
My standard [sieve of eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), an implementation of the [Miller-Rabin primality test](http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test) and method to [concatenate two numbers](http://www.mathblog.dk/files/euler/Problem60.cs).
|
||||
My standard [sieve of eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes), an implementation of the [Miller-Rabin primality test](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test) and method to [concatenate two numbers](http://www.mathblog.dk/files/euler/Problem60.cs).
|
||||
|
||||
In the end is to say that in befunge the program size is normally an good indicator for the runtime (not really, but its kinda correct for all my programs).
|
||||
So as you probably guessed this program takes a pretty loooooong time to complete.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Luckily there is a simple formula to generate [Pythagorean triples](http://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple).
|
||||
Luckily there is a simple formula to generate [Pythagorean triples](https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple).
|
||||
|
||||
~~~
|
||||
a = k * (m*m - n*n);
|
||||
|
||||
@@ -31,7 +31,7 @@ x % 3 == 2
|
||||
**Sources:**
|
||||
- [ask-math.com](http://www.ask-math.com/properties-of-square-numbers.html)
|
||||
- [johndcook.com](http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/)
|
||||
- [stackoverflow.com](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
|
||||
- [stackoverflow.com](https://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
|
||||
|
||||
If none of this pre-conditions is true we have to manually test the number.
|
||||
We use the same the same [integer-squareroot](https://en.wikipedia.org/wiki/Integer_square_root) algorithm as in previous problems.
|
||||
|
||||
Reference in New Issue
Block a user