This is a motivational talk about practicing programming skills as a professional programmer. I gave the talk many times in many conferences and companies.
You can find the video of me giving the talk on this here (in Chinese): http://v.youku.com/v_show/id_XMzI3OTI1MDQw.html
I did the talk in English as well, but there's no video online.
10. ProjectEuler.net
"Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with
an interest in the fascinating world of mathematics."
? Project Euler is a series of challenging mathematical/computer
programming problems that requires more than just mathematical
insights to solve.
10
14. Example: Quick & Dirty
Once you solved a problem, you are entitled to join the forum for this problem.You can
share your solution with the other people who have also solved it. And the post usually
start with this sentence:!
Good coding habit?
14
16. int i, j, k, n, sum;!
int factorial[10000];!
"
? It¡¯s too simple for
?
?
Java, Ruby or Python,
for which they can
handle big numbers by
default."
So let¡¯s look at C/C++."
Real code copied from
the forum.
int* getfactorial(int n)!
{!
factorial[0] = 1;!
k = 0;!
for(i = 2; i <= n; i++)!
{!
for(j = 0; j <= k; j++)!
factorial[j] *= i;!
for(j = 0; j <= k; j++)!
{!
if(factorial[j] >= 10)!
{!
factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;!
factorial[j] = factorial[j] % 10;!
if(j == k)!
k++;!
}!
}!
}!
return factorial;!
}!
"
int getsum(int* array, int k)!
{!
sum = 0;!
for(i = 0; i <= k; i++)!
sum += array[i];!
return sum;!
}!
"
int main()!
{!
int* factorial = getfactorial(n);!
sum = getsum(factorial, k);!
cout << "nThe sum of the digits of " << n << "! is " << sum << ".n";!
return 0;!
16
}!
19. #include<iostream>!
"
using namespace std;!
"
int i, j, k, n, sum;!
int factorial[10000];!
"
Why the big number is named ¡®factorial¡¯
in my previous implementation?
int* getfactorial(int n)!
{!
factorial[0] = 1;!
k = 0;!
for(i = 2; i <= n; i++)!
{!
for(j = 0; j <= k; j++)!
factorial[j] *= i;!
for(j = 0; j <= k; j++)!
{!
if(factorial[j] >= 10)!
{!
factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;!
factorial[j] = factorial[j] % 10;!
if(j == k)!
k++;!
}!
}!
}!
return factorial;!
}
Where¡¯s the code for big number?
19
20. #include <stdio.h>
"
#define NUMB 120
#define SIZE 1000000000
"
OK, too hard to reuse. It¡¯s not
that hard to invent the wheel again.
// 9 * 120 = 1080 total digits.
// 9 digit numbers
int main() {
int i = 0;
int j = 0;
int bigNum1[NUMB];
int bigNum2[NUMB];
int bigNum3[NUMB];
int counter = 0;
"
"
"
"
}
for (i = 0;
bigNum1 =
bigNum2 =
bigNum3 =
}
i < NUMB; i++) {
0;
0;
0;
bigNum1[0] = 1;
bigNum2[0] = 1;
counter = 2;
i = 0;
while (i == 0) {
counter++;
for (j = 0; j < NUMB; j++) {
bigNum3[j] = bigNum2[j] + bigNum1[j];
}
for (j = 0; j < NUMB-1; j++) {
while (bigNum3[j] >= SIZE) {
bigNum3[j] -= SIZE;
bigNum3[j+1]++;
}
}
if (bigNum3[111] >= 1)
break;
for (j = 0; j < NUMB; j++) {
bigNum1[j] = bigNum2[j];
bigNum2[j] = bigNum3[j];
}
}
printf("n");
printf("P025 answer = %u", counter);
20
21. s
ng exercise
i
oblem so lv
Pr
tant skills
ver y impor
? Train
inter view
s
ten use d a
Of
?
ques tions
n skills in
i
ly also tra
? Probab
able co de,
n
ng maintai
maki
a little.
?Don¡¯t do it before bed time...
21
36. Poker Hands in ProjectEuler
? This is not a very hard problem to solve,
?
comparing to most of the problems listed in
ProjectEuler.net"
But it¡¯s hard to solve it without bugs if you don¡¯t
have unit tests."
? Problem 54 in projectEuler.net
36