@@ -3,33 +3,57 @@ format ELF64 executable 9
33segment readable writable
44 result: times 20 db 0 ;empty string for printing the result later
55 db 10 , 0
6- p: rd 101
6+ p: rd 100000
77
88segment readable executable
99 entry start
1010
1111start:
12- mov dword [ p ], 1 ;set p[0] to 1
12+ mov dword [ p ], 1 ;p[0] = 1
1313 xor edi , edi ;init n
1414
15- sum_outer :
15+ next_n :
1616 inc edi ;n++
17- mov esi , edi ;m = n
18-
19- sum_inner:
20- push rsi
21- sub esi , edi ;m - n
22- mov eax , [ p + 4 * esi ] ;p[m - n]
23- pop rsi
24- add [ p + 4 * esi ], eax ;p[m] += p[m - n]
25- inc esi ;m++
26- cmp esi , 100 ;end of p?
27- jle sum_inner ;if not, continue inner loop
28- cmp edi , 99
29- jl sum_outer
17+ xor esi , esi ;init m
18+ mov ebx , 1 ;init gpn(n) (generalized pentagonal number)
19+
20+ p_loop:
21+ mov eax , [ p + 4 * edi ] ;p[n]
22+ mov ecx , edi
23+ sub ecx , ebx ;n - gpn(m)
24+ mov ebx , [ p + 4 * ecx ] ;p[n - gpn(m)]
25+ test esi , 2 ;m mod 4 > 1?
26+ jz continue ;if not, jump to continue
27+ neg ebx ;else negate ebx
28+
29+ continue:
30+ add eax , ebx ;p[n] +/- p[n - gpn(m)]
31+ mov ebx , 1000000
32+ cdq
33+ idiv ebx
34+ mov [ p + 4 * edi ], edx ;result mod 1e6 in p[n]
35+ inc esi ;m++
36+ mov eax , esi ;get new gpn(m):
37+ shr eax , 1
38+ inc eax ;k = (m / 2) + 1
39+ mov ebx , eax
40+ imul ebx , ebx
41+ imul ebx , 3 ;3 * k * k
42+ test esi , 1 ;m even?
43+ jnz odd ;if not, jump to odd
44+ neg eax ;if yes, negate eax
45+
46+ odd:
47+ add ebx , eax
48+ shr ebx , 1 ;gpn(m) = ((3 * k * k) +/- k) / 2
49+ cmp ebx , edi
50+ jle p_loop
51+ mov eax , [ p + 4 * edi ] ;p[n] = 0 (i. e. p[n] was divisible by 1e6)?
52+ test eax , eax
53+ jnz next_n ;if not, continue with next n
3054
3155finished:
32- mov eax , [ p + 400 ] ;result in p[100]
56+ mov eax , edi
3357 mov ebx , 10
3458 mov ecx , 19
3559
0 commit comments