in reply to porting C code to Perl
Hello
I commented some parts of the code to make it clearer what the C code is trying to do.
#define N 100 int len = floor(10 * N/3) + 1; // define an array, in C the size must be specified during compile // time (just use @a and let the loop populate the array with <len> // elements) int A[len]; for(int i = 0; i < len; ++i) { A[i] = 2; } int nines = 0; int predigit = 0; for(int j = 1; j < N + 1; ++j) { // iterate over the reversed array, transform each value // and calculate q -> 'map {...} reverse @a' ? int q = 0; for(int i = len; i > 0; --i) { int x = 10 * A[i-1] + q*i; A[i-1] = x % (2*i - 1); q = x / (2*i - 1); } A[0] = q%10; q = q/10; if (9 == q) { ++nines; } else if (10 == q) { printf("%d", predigit + 1); // print '0'x$nines; for (int k = 0; k < nines; ++k) { printf("%d", 0); } predigit, nines = 0; } else { printf("%d", predigit); predigit = q; if (0 != nines) { // print '9'x$nines; for (int k = 0; k < nines; ++k) { printf("%d", 9); } nines = 0; } } } printf("%d", predigit);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: porting C code to Perl
by marioroy (Prior) on Oct 24, 2017 at 03:11 UTC | |
by Monk::Thomas (Friar) on Oct 24, 2017 at 11:43 UTC | |
by marioroy (Prior) on Oct 24, 2017 at 14:33 UTC | |
by Monk::Thomas (Friar) on Oct 24, 2017 at 15:38 UTC | |
by RonW (Parson) on Oct 25, 2017 at 20:47 UTC | |
by marioroy (Prior) on Oct 24, 2017 at 15:57 UTC | |
by Anonymous Monk on Oct 24, 2017 at 17:53 UTC | |
by danaj (Friar) on Oct 26, 2017 at 01:56 UTC | |
by haukex (Archbishop) on Oct 24, 2017 at 18:00 UTC | |
by Monk::Thomas (Friar) on Oct 25, 2017 at 08:28 UTC | |
by haukex (Archbishop) on Oct 25, 2017 at 10:14 UTC |