Magic constant: Difference between revisions

added AWK
(add FreeBASIC)
(added AWK)
Line 40:
 
=={{header|Basic}}==
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f MAGIC_CONSTANT.AWK
# converted from FreeBASIC
BEGIN {
printf("The first 20 magic constants are:")
for (i=1; i<=20; i++) {
printf(" %d",a(i))
}
printf("\n")
printf("The 1,000th magic constant is: %d\n",a(1000))
for (i=1; i<=20; i++) {
printf("10^%02d: %8d\n",i,inv_a(10^i))
}
exit(0)
}
function a(n) {
n += 2
return(n*(n^2+1)/2)
}
function inv_a(x, k) {
while (k*(k^2+1)/2+2 < x) {
k++
}
return(k)
}
</lang>
{{out}}
<pre>
The first 20 magic constants are: 15 34 65 111 175 260 369 505 671 870 1105 1379 1695 2056 2465 2925 3439 4010 4641 5335
The 1,000th magic constant is: 503006505
10^01: 3
10^02: 6
10^03: 13
10^04: 28
10^05: 59
10^06: 126
10^07: 272
10^08: 585
10^09: 1260
10^10: 2715
10^11: 5849
10^12: 12600
10^13: 27145
10^14: 58481
10^15: 125993
10^16: 271442
10^17: 584804
10^18: 1259922
10^19: 2714418
10^20: 5848036
</pre>
==={{header|FreeBASIC}}===
<lang freebasic>
477

edits