oezg
October 21, 2025, 10:22am
1
The solution that uses integer arithmetic and relies on big decimal support does not work in the exercise. Do you have any explanation?
BEGIN {
for (temp = num; temp; temp = int(temp / 10)) digits[i++] = temp % 10
for (j = 0; j < i; j++) temp += digits[j] ^ i
print num == temp ? "true" : "false"
}
The exercise description mentions that the -M option should work.
glennj
October 21, 2025, 1:16pm
2
I added some debugging statements, and it looks like the digits are not getting created properly
print num > "/dev/fd/3"
for (temp = num; temp; temp = int(temp / 10)) {
digits[i++] = temp % 10
print i, temp > "/dev/fd/3"
}
print "--" > "/dev/fd/3"
for (j = 0; j < i; j++) {
print j, digits[j] > "/dev/fd/3"
temp += digits[j] ^ i
}
print temp > "/dev/fd/3"
outputs
86709961001538790100634132976990
1 186709961001538790100634132976990 0
2 18670996100153879010063413297699 9
3 1867099610015387799844860985344 4
4 186709961001538772947611680768 8
5 18670996100153876415151865856 6
6 1867099610015387531564023808 8
7 186709961001538766900297728 8
8 18670996100153877549023232 2
9 1867099610015387701215232 2
10 186709961001538783543296 6
11 18670996100153878773760 0
12 1867099610015387877376 6
13 186709961001538781184 4
14 18670996100153876480 0
15 1867099610015387648 8
16 186709961001538752 2
17 18670996100153876 6
18 1867099610015387 7
19 186709961001538 8
20 18670996100153 3
21 1867099610015 5
22 186709961001 1
23 18670996100 0
24 1867099610 0
25 186709961 1
26 18670996 6
27 1867099 9
28 186709 9
29 18670 0
30 1867 7
31 186 6
32 18 8
33 1 1
96528163564384799588342974550580
not ok 10 Armstrong number containing seven zeroes
It seems that int(temp / 10) isn’t working nicely with the arbitrary precision arithmetic.
Spoiler: handle the input as a string to get the digits
glennj
October 21, 2025, 1:18pm
3
Looking in the gawk manual, now I see this, unfortunately.
oezg
October 22, 2025, 10:12am
4
Thank you for debugging it. I also noticed the problem. Maybe we should consider removing the comment and the -M option from these tests:
# The next 2 tests require the -M option to handle big numbers.
# Refer to the instructions of the Grains exercise:
# https://exercism.org/tracks/awk/exercises/grains
@test "Armstrong number containing seven zeroes" {
run gawk -M -f armstrong-numbers.awk -v num=186709961001538790100634132976990
assert_success
assert_output "true"
}
@test "The largest and last Armstrong number" {
run gawk -M -f armstrong-numbers.awk -v num=115132219018763992565095597973971522401
assert_success
assert_output "true"
}
glennj
October 22, 2025, 12:17pm
5
The choice isn’t to remove -M or not, it’s to remove those tests or not. They won’t work without it: the sum cannot be successfully accumulated.
Perhaps the answer is to write up some more about it in an instructions.append file.
2 Likes