Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add armstrong-numbers exercise #205

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "armstrong-numbers",
"name": "Armstrong Numbers",
"uuid": "1a5daac4-6572-4cb0-8cd7-ad27e69dd2c6",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "hamming",
"name": "Hamming",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/armstrong-numbers/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hints

## General

- The `$t0-9` registers can be used to temporarily store values
- The instructions specify which registers are used as input and output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Instructions append

## Registers

| Register | Usage | Type | Description |
| -------- | --------- | ------- | ------------------------------------------------- |
| `$a0` | input | integer | number |
| `$v0` | output | boolean | is armstrong number (`0` = `false`, `1` = `true`) |
| `$t0-9` | temporary | any | used for temporary storage |
14 changes: 14 additions & 0 deletions exercises/practice/armstrong-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions

An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number
19 changes: 19 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"blurb": "Determine if a number is an Armstrong number.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
}
56 changes: 56 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Determine if a given number is an Armstrong number.
#
# $a0 - input number
# $v0 - final value - is $a0 an Armstrong number?
# $t0 - 10
# $t1 - original stack pointer
# $t2 - number of digits
# $t3 - remaining exponent
# $t4 - quotient, input number with lowest digits removed
# $t5 - individual digit
# $t6 - digit raised to a power
# $t7 - lowest bit of exponent

.globl is_armstrong_number

.text

is_armstrong_number:
move $t1, $sp # preserve stack pointer
li $t0, 10 # prepare to extract decimal digits
move $t4, $a0 # copy input number

extract_digit:
divu $t4, $t0
mfhi $t5 # Remainder is last decimal digit
subi $sp, $sp, 4 # Push digit $t5 onto the stack
sw $t5, 0($sp)
mflo $t4 # Quotient represents leading digits
bne $t4, $zero, extract_digit

sub $t2, $t1, $sp # Calculate number of digits pushed on stack
srl $t2, $t2, 2 # by dividing byte count by 4

raise_digit:
lw $t5, 0($sp) # Pop digit off the stack
addi $sp, $sp, 4
move $t3, $t2 # num digits
li $t6, 1

multiply:
andi $t7, $t3, 1 # Check if the exponent is odd
beq $t7, $zero, square
mul $t6, $t6, $t5

square:
mul $t5, $t5, $t5 # Square the base
srl $t3, $t3, 1 # Halve the exponent
bne $t3, $zero, multiply

sub $a0, $a0, $t6 # Subtract digit power from original number
bne $sp, $t1, raise_digit # Repeat until all digits removed from stack.

seq $v0, $a0, $zero # We have an Armstrong number if subtracting
# the digit powers cancels the original number.
jr $ra # Return
45 changes: 45 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c1ed103c-258d-45b2-be73-d8c6d9580c7b]
description = "Zero is an Armstrong number"

[579e8f03-9659-4b85-a1a2-d64350f6b17a]
description = "Single-digit numbers are Armstrong numbers"

[2d6db9dc-5bf8-4976-a90b-b2c2b9feba60]
description = "There are no two-digit Armstrong numbers"

[509c087f-e327-4113-a7d2-26a4e9d18283]
description = "Three-digit number that is an Armstrong number"

[7154547d-c2ce-468d-b214-4cb953b870cf]
description = "Three-digit number that is not an Armstrong number"

[6bac5b7b-42e9-4ecb-a8b0-4832229aa103]
description = "Four-digit number that is an Armstrong number"

[eed4b331-af80-45b5-a80b-19c9ea444b2e]
description = "Four-digit number that is not an Armstrong number"

[f971ced7-8d68-4758-aea1-d4194900b864]
description = "Seven-digit number that is an Armstrong number"

[7ee45d52-5d35-4fbd-b6f1-5c8cd8a67f18]
description = "Seven-digit number that is not an Armstrong number"

[5ee2fdf8-334e-4a46-bb8d-e5c19c02c148]
description = "Armstrong number containing seven zeroes"
include = false

[12ffbf10-307a-434e-b4ad-c925680e1dd4]
description = "The largest and last Armstrong number"
include = false
10 changes: 10 additions & 0 deletions exercises/practice/armstrong-numbers/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# | Register | Usage | Type | Description |
# | -------- | --------- | ------- | ------------------------------------------------- |
# | `$a0` | input | address | number |
# | `$v0` | output | boolean | is armstrong number (`0` = `false`, `1` = `true`) |
# | `$t0-9` | temporary | any | used for temporary storage |

.globl is_armstrong_number

is_armstrong_number:
jr $ra
102 changes: 102 additions & 0 deletions exercises/practice/armstrong-numbers/runner.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#
# Test is_armstrong_number with some examples
#
# s0 - num of tests left to run
# s1 - address of input word
# s2 - address of expected output word
# s4 - output word

.data

# number of test cases
n: .word 9
# input values (word sized ints)
ins: .word
0,
5,
10,
153,
100,
9474,
9475,
9926315,
9926314
# expected output values (word sized ints)
outs: .word
1,
1,
0,
1,
0,
1,
0,
1,
0

failmsg: .asciiz "failed for test input: "
expectedmsg: .asciiz ". expected "
tobemsg: .asciiz " to be "
okmsg: .asciiz "all tests passed"


.text

runner:
lw $s0, n
la $s1, ins
la $s2, outs
j run_test

run_test:
lw $a0, 0($s1) # move input number to a0
jal is_armstrong_number # call subroutine under test
move $v1, $v0 # move return value in v0 to v1 because we need v0 for syscall

lw $s4, 0($s2) # read expected output from memory
bne $v1, $s4, exit_fail # if expected doesn't match actual, jump to fail

addi $s1, $s1, 4 # move to next word in input
addi $s2, $s2, 4 # move to next word in output

sub $s0, $s0, 1 # decrement num of tests left to run
bgt $s0, $zero, run_test # if more than zero tests to run, jump to run_test

exit_ok:
la $a0, okmsg # put address of okmsg into a0
li $v0, 4 # 4 is print string
syscall

li $v0, 10 # 10 is exit with zero status (clean exit)
syscall

exit_fail:
la $a0, failmsg # put address of failmsg into a0
li $v0, 4 # 4 is print string
syscall

lw $a0, ($s1) # print input that failed on
li $v0, 1
syscall

la $a0, expectedmsg
li $v0, 4
syscall

move $a0, $v1 # print actual that failed on
li $v0, 1
syscall

la $a0, tobemsg
li $v0, 4
syscall

move $a0, $s4 # print expected value that failed on
li $v0, 1
syscall

li $a0, 1 # set error code to 1
li $v0, 17 # 17 is exit with error
syscall

# # Include your implementation here if you wish to run this from the MARS GUI.
# .include "impl.mips"