title: R.E.P.A.I.R. T.H.E. K.E.Y.B.O.A.R.D

text:
# Question
Title: R.E.P.A.I.R. T.H.E. K.E.Y.B.O.A.R.D
Sequel of this AWESOME challenge

You, a genius tech prodigy detective, have successfully disarmed the bomb and want to break into the computer. The police found the password `67890-=yuiop[]hjkl;'\vbnm^&*()_+:"|` on a piece of paper, but much of the keyboard is broken and there are no other ways of inputting characters.

The only characters left to use are `12345QWERTASDFG<>ZXCqwertasdfgzxc!@#$%` and also Tab, Space, and Newline. However, a fairy appears and gives you one more character of your choice.

Using only these 42 characters, produce as many of the password's 35 characters as you can. Write a separate program/function for each password character.

### Scoring:

1. Primary scoring is how many characters you can produce (1–35).
2. Secondary, tie-breaking score is the total length of your programs/functions.

Standard loopholes apply

*Thanks to @Adám for rework*

# Answer
# Deadfish, all 35 characters, 444 bytes

Uses `s`, `d`, and `c`, which are allowed, plus `i` as an extra.

```
6 iisiiisiiiiic
7 iisiiisiiiiiic
8 iisiiisiiiiiiic
9 iiisdsdddddddc
0 iisiiisdc
- iisiiisddddc
= iiisdsdddc
y iiisiisc
u iiisiisddddc
i iiisisiiiiic
o iiisisiiiiiiiiiiic
p iiisiisdddddddddc
[ iiissiiiiiiiiiic
] iiisisdddddddc
h iiisisiiiic
j iiisisiiiiiic
k iiisisiiiiiiic
l iiisisiiiiiiiic
; iiisdsdddddc
' iisiisiiic
\ iiisisddddddddc
v iiisiisdddc
b iiisisddc
n iiisisiiiiiiiiiic
m iiisisiiiiiiiiic
^ iiisisddddddc
& iisiisiic
* iisiisiiiiiic
( iisiisiiiic
) iisiisiiiiic
_ iiisisdddddc
+ iisiisiiiiiiic
: iiisdsddddddc
" iisiisddc
| iiisiisiiic

```

Unless I did my coding wrong, I believe these are necessarily optimal.

Here is the meta-golfer

## Explanation

I do a pretty simple BFS by creating a priority queue that orders by length first (so the golfiest solution is always found) and state second (so that it doesn't waste time on ridiculously large numbers caused by repeated application of `s`). Also, I track a visited set to prevent revisiting values with suboptimal-length code.

Then, each time, I just add the state transitions `(L,x,c)->(L+1,x+1,c+"i")`, `(L,x,c)->(L+1,x-1,c+"d")`, and `(L,x,c)->(L+1,x*x,c+"s")` where `L` is the code length, `x` is the current number, and `c` is the current code.

My meta-golfer code is fully commented if you want a more detailed explanation.
> 20 votes

# Answer
# Javascript, score 5

```
6: x=>21%15
7: x=>22%15
8: x=>23%15
9: x=>24%15
0: x=>1%1

```

```
12345QWERTASDFG<>ZXCqwertasdfgzxc!@#$%

```

# Step 1: Functions or programs.

We need to have functions, so we have to take `=`, from which we can make `x=>[code]` or similar.

So, what can we do with what we've got?

67890 can be made with numeric operations. But for the rest, we need string coercion, which we can't get without some other character.

# What if I didn't need functions? (Note - below this is all hypothetical).

We still need some form of string coercion:

* String.fromCharCode
* `+[]` or `+''`
* Literally calling toString

The first requires `in.omh`, so is obviously not an option.

The second requires `+` and either `'`,`"`, a backtick or `[]` \- Sorry, but no.

The third has the same problems as the first.

So that's all we can do, and it's not enough.

# But what about 2 characters?

Sadly, `+'` and `+"` have no way of getting specific characters. +\` adds function calling to our repetoire, but charAt needs an h and a . .

If we start with a string(using backticks because function calling), we can get one character of our choice! But we can't get anything else, so that's a dead end.

Enter escape sequences!

With `'` and `\`, we can do things like `"\153"` or `"\x2c"`, netting us a bunch of extra characters! The complete list is:

```
-: '\55'
i: '\151'
[: '\133'
]: '\135'
j: '\152'
k: '\153'
l: '\154'
b: '\142'
m: '\155'
*: '\52'
): '\51'
+: '\53'
": '\42'
=: '\x3d'
]: '\x5d'
^: '\x5e'
_: '\x5f'
:: '\x3a'
Along with the standard:
': '\''
\: '\\'

```

This leaves `yuoph;n&(|` to deal with.

Checking which character will be the most effective out of 67890b, we get 7, adding 5 characters to our list

```
y: '\171'
u: '\x75'
o: '\157'
;: '\73'
|: '\174'

```

And now we're down to `phn&(`. Checking again, we find 0 gives us `ph(`:

```
p: '\x70'
h: '\150'
(: '\50'

```

Leaving us `n&`. Finally, 6 unlocks the last two:

```
n: '\156'
&: '\46'

```

So our extra charset is `'\670`. Note that here we have assembled the full octal set: `01234567`, and can make any character with what we've defined. But what else can we do?

# What if we try something different?

What if we use `+[]` to start with? `+[]` is the basis of most of javascript, and we can now use some previously useless characters. We still have no function calling, but:

We can get `.` by taking the second character of 11e111 which is coerced into 1.1e112: `[11e111+[]][+[]][1]`

From there, we can get `-`, by making .0000001 as a string, coercing into a float, which is 1e-7, then coerce back to a string and get the right character : `[+[[11e111+[]][+[]][1]+1%1+1%1+1%1+1%1+1%1+1%1+1][+[]]+[]][+[]][2]`

We can get the string "undefined" from taking the first of an empty array: `[][+[]]+[]`, "true" and "false" from `!![]+[]` and `![]+[]`, "NaN" from coercing undefined to a number: `+[][+[]]`, and "\[object Object\]" from `CSS+[]`. Adding this, our character pool (as strings only) is now:

"-.undefitrualsNa\[objcO\]"

We can take `[]['f'+'i'+'n'+'d']+[]` to get the code of the find function as a string:

```
function find() {
    [native code]
}

```

Our string character pool is now `-.undefitrualsNa[objcO] (){vc}` and newline.

Note that from this we can get "constructor". We can use this like so: `[]["constructor"]+[]` returns `function Array() { \n[native code]\n}`, adding A and y to our pool. `[[]+[]][+[]]["constructor"]+[]` returns the string function, giving us S and g. `[]["find"]["constructor"]+[]` adds F from Function.

So, our current string is `-.undefitrualsNa[objcO] (){vc}AySgF`, from which we can take snippets for `-yuio[]jlvbn()` by taking arbitrary characters, leaving us with `=phk\m^&*_+:"|` to deal with. Note that while `+` is in our codebase, it cannot be coerced into a string with only what we have.

# Now what?

For the remaining characters, one option is to add `()`, reducing it to JSFuck.

Another option is to add backtick and `=`, reducing it to Turing complete javascript

With the new Pipeline operator, all you need is `|` (we already have `>`), but does this count as valid?

We can also try the afromentioned escape sequences.

## In conclusion, in Javascript is very difficult.
> 17 votes

# Answer
# Labyrinth, score 35, 180 bytes

```
6 54.@
7 55.@
8 312.@
9 313.@
0 !@
- 45.@
= 1341.@
y 121.@
u 1141.@
i 2153.@
o 111.@
p 112.@
[ 1115.@
] 2141.@
h 2152.@
j 2154.@
k 1131.@
l 1132.@
; 315.@
' 551.@
\ 4444.@
v 4214.@
b 354.@
n 1134.@
m 1133.@
^ 2142.@
& 2342.@
* 42.@
( 552.@
) 41.@
_ 351.@
+ 43.@
: 314.@
" 34.@
| 124.@

```

Try the comprehensive test online!

Uses `.` (character output command) from the fairy.

## Explanation

A Labyrinth program is normally laid out on a 2D grid, but here each program is supposed to be a no-input single-char-output program, so they're best golfed as linear programs.

Now, except for `!@` which outputs `0`, every program constructs a single number, outputs it as a char, and halts.

### Secret 1

`0-9` commands in Labyrinth are not number literals. Instead, a digit command adds itself to the end of the top number on the stack (alternatively, multiply 10 and add the digit to the top). Since the top is implicitly zero at the start of the program, a sequence of digit commands constructs that exact multi-digit number.

### Secret 2

The real magic comes with the `.` command, which prints the top number **modulo 256** as char. So `312.` gives `8` (ascii 56) because `312 % 256 == 56`. It turns out that all charcodes can be converted to an equivalent number modulo 256 that has only 1-5 in its digits.

---

The answer in Hexagony, initially written in the similar vein, has been moved to another answer of its own.
> 14 votes

# Answer
# Sleep, all 35 chars, 5292 bytes

```
6 zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zz zzz zzzzz
7 zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzz zzzzz zz zzz zzzzz
8 zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz z zzzz zzzz zzzz zzzz zzzzz zz zzz zzzzz
9 zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz z zzzz zzzz zzzz zzzzz zz zzz zzzzz
0 zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz z zzzzz zz zzz zzzzz
- zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
= zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
y zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
u zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
i zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzz zzz zzz zzzzz
o zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
p zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzzzz
[ zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
] zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzzzz
h zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
j zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzz zzzzz
k zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzzz
l zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
; zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz z zzz zzz zzz z zzz zzz zzz zzz zzz zz zz zz zz zzzz zzzzzzzz z zzz zzzzz z zzzzz zz zzz zzz zzz zzz zzzzz z zzz zzz zzz zzz zzz zzz zzzzz zzz zzz zzzzz z zzz zzz zzz zzz zzzzz zzzz zzzzz z zzzz zzzzz zz zzzzz zzz zzz zzzzz
' zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzzz
\ zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
v zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
b zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
n zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
m zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
^ zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzz zzzzz
& zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzzzz
* zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
( zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzzz
) zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
_ zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzzz
+ zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzzzz
: zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzzzz
" zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzzz zzzzz
| zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzzzzzz z zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zz zzzz zzzzzzzz z zzz zzz zzz zzzzz

```

Each line is a program. First two letters are comments and should be removed from source code when you try to run it.

You just need to find out a brainfuck like language only use valid charset to get the power of turing complete...

I know I can golf more bytes. But I'm too lazy to working on it any more.
> 11 votes

# Answer
# Deoxyribose, all 35 characters in 753 bytes

```
6 ATGCATTCGCGTTAA
7 ATGCATTCTCGTTAA
8 ATGCATTGACGTTAA
9 ATGCATTGCCGTTAA
0 ATGCATTAACGTTAA
- ATGCATGTCCGTTAA
= ATGCATTTCCGTTAA
y ATGCATTTTCATTGGGGTCTTCGTTAA
u ATGCATTTTCATTCGGGTCTTCGTTAA
i ATGCATTTTCATGGGGGTCTTCGTTAA
o ATGCATTTTCATTAAGGTCTTCGTTAA
p ATGCATTTTCATTACGGTCTTCGTTAA
[ ATGCATTTTCATCTAGGTCTTCGTTAA
] ATGCATTTTCATCTGGGTCTTCGTTAA
h ATGCATTTTCATGGCGGTCTTCGTTAA
j ATGCATTTTCATGGTGGTCTTCGTTAA
k ATGCATTTTCATGTAGGTCTTCGTTAA
l ATGCATTTTCATGTCGGTCTTCGTTAA
; ATGCATTGTCGTTAA
' ATGCATGCTCGTTAA
\ ATGCATTTTCATCTCGGTCTTCGTTAA
v ATGCATTTTCATTCTGGTCTTCGTTAA
b ATGCATTTTCATGATGGTCTTCGTTAA
n ATGCATTTTCATGTTGGTCTTCGTTAA
m ATGCATTTTCATGTGGGTCTTCGTTAA
^ ATGCATTTTCATCTTGGTCTTCGTTAA
& ATGCATGCGCGTTAA
* ATGCATGGGCGTTAA
( ATGCATGGACGTTAA
) ATGCATGGCCGTTAA
_ ATGCATTTTCATGAAGGTCTTCGTTAA
+ ATGCATGGTCGTTAA
: ATGCATTGGCGTTAA
" ATGCATGAGCGTTAA
| ATGCATTTTCATTTCGGTCTTCGTTAA

```

Since A, C, G, and T were all allowed, how could I resist? I didn't need anything from the fairy.

* The `ATG` at the start of each line is the necessary start codon
* Then, this just pushes the codepoint of each character to the stack (one on each line) in the most inefficient possible way; each `CATxxx` pushes an integer up to 63, then each `GGTCTT` is an addition
* It then pops them straight out as Unicode characters (`CGT`)
* Each line ends with a stop codon (`TAA`), without which the program will just repeat forever

This could definitely get a lot shorter if I tried, but this isn't golf.
> 9 votes

# Answer
# Pyth, score 35, ~~147~~ 139 bytes

I have asked the fairy to give me the character "9".

Try all of these online!

Verify!

# Explanation

```
Code       Char  Expl
C54        # 6   # Character with value 54
C55        # 7   #
%44 9      # 8   # 44 % 9
9          # 9   #
Z          # 0   # Initialized to 0 by default.
C45        # -   # Character with value 45
C192 %131  # =   # First take 192 % 131, then print character of that value
C121       # y   # Character with value 121
@tG19      # u   # G is initialized to "abcdefghijklmnopqrstuvwxyz", tail that and print char at index 19
@gG5 4     # i   # g is used for inclusive slice to remove characters before index 5.
@G14       # o   # ...
C112       # p   # Character with value 112
C91        # [   # ...
C93        # ]   # ...
@ttG5      # h   # ...
@G9        # j   # ...
@tG9       # k   # ...
@G11       # l   # ...
C59        # ;   # ...
C39        # '   # ...
C92        # \   # ...
@G21       # v   # ...
@G1        # b   # ...
@G13       # n   # ...
@G12       # m   # ...
C94        # ^   # ...
C%93 55    # &   # ...
C42        # *   # ...
C%95 55    # (   # ...
C41        # )   # ...
C95        # _   # ...
C43        # +   # ...
C%199 141  # :   # ...
C34        # "   # ...
C124       # |   # ...

```

I used the similar trick for most character so I did not repeat the explanations.
> 8 votes

# Answer
# Zsh \+ xxd, score 35, 478 bytes, fairy character `-`

```
tr AA 5-A<<<A
tr AAA 5-A<<<A
tr A-Z 5-A<<<D
tr @-Z 5-A<<<D
<<<$#
<<<-
tr R-Z 5-A<<<Z
tr 12 x-z<<<2
tr 12 t-z<<<2
tr 123 g-z<<<3
tr R-Z g-z<<<Z
tr Q-Z g-z<<<Z
tr 12 Z-a<<<2
tr 1-4 Z-a<<<4
tr 12 g-z<<<2
tr 1-4 g-z<<<4
tr 1-5 g-z<<<5
tr A-G g-z<<<F
tr A-Z 5-A<<<G
tr 123 %-1<<<3
tr 123 Z-a<<<3
tr 123 t-z<<<3
tr 12 a-z<<<2
tr Q-Z g-z<<<X
tr A-G g-z<<<G
tr 1-5 Z-a<<<5
tr 12 %-1<<<2
tr A-Z %-1<<<F
tr A-Z %-1<<<D
tr @-Z %-1<<<D
tr A-X Z-a<<<F
tr A-Z %-1<<<G
tr A-Z 4-A<<<G
tr 12 !-Z<<<2
xxd<<<r>f
tr 2 c<f>g
xxd -r<g

```

Try it online!

Each program is on a separate line, except the last one (`|`) which spans 3 lines. `-`, which the fairy has given us, is a pretty essential character for programming in shell languages :P

`tr` (for **tr**anslate characters) turns out to be extremely useful here - most of these characters are easily obtained using 5 minutes and an ASCII chart, by creating a range that surrounds the character we want and "indexing" into it by translating a character.

`<<<`, "here-string redirection", pipes a single word into the command (`command <<< word` \\$ \equiv \\$ `echo word | command`). This allows us to give input to `tr`, and if no command is given, it prints to stdout, so we can print literal strings like `-` too. Also, `$#` is the number of inputs, which is naturally 0 for a challenge like this.

Unfortunately, `|` can't be made with the `tr` technique because there are no accessible characters after it in ASCII to create a range around.

The available commands with our charset is limited, but I found `xxd`, which is a hexdump tool bundled with vim. The general idea is to create a hexdump of a single character that's *close* to `|`, translate some the hex characters to make them *actually* `|`, and un-hexdump (hexload?) it again.

```
# get a hexdump of `r` and write to the file `f`
xxd<<<r>f
# translate `2` to `c`, reading from the file `f` and writing to `g`
tr 2 c<f>g
# un-hexdump from the file `g`
xxd -r<g

```

In this case, we use `r` (0x72) and translate the `2` to a `c` for 0x7C (`|`). Since we can't pipe the commands into each other, we must redirect `>` to a file `f` and then read from it again with `<`, and do this again with another file `g`.

There may be a different approach to be taken here that uses more common commands and is shorter.
> 7 votes

# Answer
# shortC, score 35, 184 bytes

```
AP54
AP55
AP312
AP313
AP2352
AP45
AP1341
AP121
AP1141
AP2153
AP111
AP112
AP1115
AP2141
AP2152
AP2154
AP1131
AP1132
AP315
AP551
AP4444
AP1142
AP354
AP1134
AP1133
AP2142
AP2342
AP42
AP552
AP41
AP351
AP43
AP314
AP34
AP124

```

Uses `P` from the fairy.

## How it works

`A` is converted to `int main(int argc, char **argv){`

`P`is converted to `putchar(` which outputs the character passed as argument after converting it to an unsigned char, which is equivalent to take the modulo 256.

E.g. `AP54` is converted to something equivalent to:

```
int main(int argc, char **argv){
	putchar(54);
}

```

Generated with this script
> 6 votes

# Answer
# HTML (w3m), 18 char

with an extra `&`

```
&#54
&#55
&#45
&#121
&#111
&#112
&#42
&#41
&#43
&#34
&#124
&#x3d
&#x5d
&#x5c
&#x5e
&#x5f
&#x3a
&

```

It is possible to output `6`, `7`, `-`, `y`, `o`, `p`, `*`, `)`, `+`, `"`, `|`, `=`, `]`, `\`, `^`, `_`, `:`, `&`.
> 5 votes

# Answer
# Cubically, all 35 characters in 166 bytes.

(162 bytes possible; see third-to-last paragraph.)

*Cubically is by PPCG user MD XF.*

Each line is a Cubically program; omit the first two characters. Try it online!

```
//accessible via 3x3x3 cube
- @5
' F@5
& FR@5
* R@5
( DDR@5
) DR@5
+ FRF3@5   // F3 is valid Cubically for FFF, saving 1 byte
" DRF@5

//requires 4x4x4 cube (invoke with flag -4)
6 DFD@4
7 FRR@5
8 DD@4
9 FD@4
0 @3
= FFRR@5
; DRDR@5
: FFD@4

//requires 5x5x5 cube (invoke with flag -5)
y DRER3@5
u DEDR@5
i FS@5
o SER@5
p FRD@5
[ SDRR@5
] FRR@5
h FES@5
j DRF@5
k DRDS@5
l FRF@5
\ FD@4
v DDR@5
b DRDR@5
n DSR@5
m DR3@5
^ FDF@5
_ E@4
| FEF3@5

```

## Explanation

Cubically manipulates values on the surface of a Rubik's cube. (There is a sixth, off-cube memory location accessible via the command `:` from the keyboard's right half; as the arithmetic operators are also blown away there's not much use for it. Fairy: take a break.)

Accessible to us on the left-hand side of the keyboard are commands to rotate the (R)ight face clockwise 90 degrees, the (D)ownward face, the (F)ront face, the middle layer inward from the bottom (E), and the middle layer inward from the front (S). And, crucially, the @ command which outputs the subsequent memory location as an ASCII character. Finally, the sums of values across each face occupy memory values 0-5. So a command of `@3` sums the values on face 3 and outputs the corresponding ASCII character.

A 3x3x3 cube can have maximum face-value 45, 4x4x4 maxes out at 80, and 5x5x5 at 125, so we'll need at least a 5x5x5 cube to reach required ASCII values. On a 5x5x5 `D@1` produces the same output as `DER@5` on a 3x3x3, so one could golf 2 bytes there by using a larger cube than *necessary* to produce `)`. But at that point it started to feel like I was golfing on invocation argument rather than on code, so I've restricted myself to the smallest cubes *necessary* to produce each character. By allowing a 5x5x5 cube even for smaller values, it's possible to golf 4 bytes: the two mentioned above and one each from `FRR@5`-4 --\> `DE@1`-5 and `FRF3@5` --\> `SER@2`-4 producing `7` and `+`, respectively.

(It is possible to find shorter programs on larger cubes than some of these presented--for example, on a 7x7x7 `@2` produces the same output as `DRDR@5` on a 5x5x5. But that's starting to feel like complete shenanigans.)

Most of these programs have a half-dozen or more siblings that produce the same output at the same length. Simply: `D` & `E` or `F` & `S` are often interchangeable, particularly for shorter programs.
> 5 votes

# Answer
# unary , 35 chars, 15297610066607123676 bytes

0 are used as extra char, also the only char used.

I will just give how many 0 are there

```
6 415301196285076
7 3322409570280596
8 24327480129166876
9 3040935016145860
0 415301196285660
- 2913391660822676
= 3217063028519132
y 402132826660380
u 25736500906368540
i 25736500906368540
o 3040935009656340
p 24327480077250708
[ 3040908012496020
] 194618112799745172
h 380116824301076
j 24327476755268756
k 194619814042150036
l 24327480077250780
; 194619841033334932
' 194619840618098836
\ 23163646650164164
v 3217062613296068
b 25840407707137756
n 380116876207044
m 3040935009656348
^ 1556944902397961364
& 24327480077262356
* 364173957602844
( 23307133286581980
) 2913391660822748
_ 12455559219183690900
+ 45521744700356
: 24327480129166868
" 206723264985755868
| 6308640661020

```
> 4 votes

# Answer
# Retina, 188 bytes, score 19

Fairy character: ```.

```
x

```

Counts number of `x`s in the input, which is `0` (1 byte)

```

xxxxx

```

Inserts 5 `x`s and counts the `6` boundaries (7 bytes).

```

xxxxxx

```

Inserts 6 `x`s and counts the `7` boundaries (8 bytes).

```

xxx

x

```

Inserts 3 `x`s, then inserts a further `x` at each boundary, then counts the resulting `8` boundaries (8 bytes).

```

xx

xx

```

Inserts two `x`s, then inserts another two `x`s at each of the three boundaries, then counts the resulting `9` boundaries (8 bytes).

```

1
T`d`Rw

```

In the Transliterate function, `d` represents `0123456789`, `E` represents `02468` and `Rw` represents `zyx`..`_`. After inserting a `1`, this therefore transliterates the second digit into the second last letter `y` (9 bytes).

```

5
T`d`Rw

```

Inserts a `5`, then transliterates the sixth digit into the sixth last letter `u` (9 bytes).

```

x
T`ddx`xxxRw

```

Inserts an `x`, then the `x` is the 21st source character so it translates into the 17th last letter `i` (14 bytes).

```

x
T`dtx`Rw

```

Inserts an `x`, then the `x` is the 12th source character so it translates into the 12th last letter `o` (11 bytes).

```

x
T`dx`Rw

```

Inserts an `x`, then the `x` is the 11th source character so it translates into the 11th last letter `p` (10 bytes).

```

x
T`ddx`xxRw

```

Inserts an `x`, then the `x` is the 21st source character so it translates into the 19th last letter `h` (13 bytes).

```

x
T`dEtx`Rw

```

Inserts an `x`, then the `x` is the 17nd source character so it translates into the 17th last letter `j` (12 bytes).

`k`:

```

x
T`dEx`Rw

```

Inserts an `x`, then the `x` is the 16th source character so it translates into the 16th last letter `k` (11 bytes).

```

x
T`dEx`xRw

```

Inserts an `x`, then the `x` is the 16th source character so it translates into the 15th last letter `l` (12 bytes).

```

4
T`d`Rw

```

Inserts a `4`, then transliterates the fifth digit into the fifth last letter `v` (9 bytes).

```

x
T`ddEx`xRw

```

Inserts an `x`, then the `x` is the 26th source character so it translates into the 25th last letter `b` (13 bytes).

```

x
T`dttx`Rw

```

Inserts an `x`, then the `x` is the 13th source character so it translates into the 13th last letter `n` (12 bytes).

```

x
T`dtttx`Rw

```

Inserts an `x`, then the `x` is the 14th source character so it translates into the 14th last letter `m` (13 bytes).

```

x
T`x`w

```

Inserts an `x` and then transliterates it to an `_`. (8 bytes)

No other characters are possible, because the only source of arbitrary printable ASCII is via the `p` transliteration builtin.
> 4 votes

# Answer
# Jelly, 35 characters, 188 bytes

```
3!
34DS
44DS
45DS

45Ọ
212%151Ọ
121Ọ
3r15SỌ
14RSỌ
111Ọ
112Ọ
13RSỌ
31x3SỌ
104Ọ
53x2SỌ
54x2SCAỌ
54x2SỌ
12x5SCAỌ
13x3SỌ
213%121Ọ
241%123Ọ
33x3SCAỌ
111CAỌ
111CACAỌ
215%121Ọ
13x3SCAỌ
42Ọ
41CAỌ
41Ọ
24x4SCAỌ
43Ọ
211%153Ọ
34Ọ
124Ọ

```

Try it online!

Using  as the extra character. Each program is on a separate line. A lot of these are kind of last-resort and I'll try to golf them.

## Explanation

* `!` = factorial
* `D` = decimal digits
* `S` = sum
* = convert to character
* `%` = modulo
* `r` = range from m to n
* `R` = range from 1 to n
* `x` = repeat
* `C` = 1 - n
* `A` = absolute value
> 3 votes

# Answer
# Vyxal, all 35 characters, 243 bytes

```
54C
55C
55 1+C
55 2+C
45 3+C
45C
51 5d+C
121C
111 3d+C
5d2e5+C
111C
112C
5 4+2e5d+C
52 41+C
5d2e4+C
5d2e3d+C
5d2e3d+1+C
5d2e4d+C
54 5+C
35 4+C
44 2+dC
115 3+C
45 4+dC
5dD2e+C
5d2e4 5++C
45 1+d2+C
35 3+C
42C
5dddC
41C
45d5+C
43C
55 3+C
34C
124C

```

Try it Online!

The extra character is `+`
> 3 votes

# Answer
# Hexagony, score 35, ~~<sup>180</sup>~~ 152 bytes

This is split out of my own Labyrinth answer, because it now adds some more real tricks over the previous, and therefore the resulting code is sufficiently different. Refer to that answer for **Secret 1** and **Secret 2**.

```
6 54;@
7 55;@
8 R4;@
9 R5;@
0 !@
- 45;@
= R53;@
y r5;@
u r1;@
i W3;@
o q5;@
p 112;@
[ Z51;@
] Z53;@
h W2;@
j W4;@
k W5;@
l q2;@
; 315;@
' 551;@
\ Z52;@
v r2;@
b 354;@
n q4;@
m q3;@
^ t14;@
& W42;@
* 42;@
( 552;@
) 41;@
_ 351;@
+ 43;@
: 314;@
" 34;@
| 124;@

```

Try the comprehensive test online!

The exact same construction as the Labyrinth answer also works with Hexagony, because the two languages share the digit commands and the character output behavior. The output command is `;` (unlocked with fairy). But Hexagony has ~~one more quirk~~ **two** more quirks. One makes it so that the (almost) same code only works by coincidence:

### Secret 3

A Hexagony program is laid out on the smallest hexagonal grid that fits the entire source code before running. For the programs of size 2 to 6, the hexagon side length is 2:

```
 A B
C D E
 F G

```

The IP starts at A, facing right. When it exits through B, it re-enters through C. When it exits through E, the current value is checked, and fortunately it is always positive (since the constant set up is still there), so it re-enters through F. Therefore, the IP flows through ABCDEFG, exactly in the linear order.

The other actually contributes to golfing the code further:

### Secret 4

In addition to the digit commands, the uppercase/lowercase letters in Hexagony are also valid commands, which simply set the current memory cell to its own ASCII value. And we have half of those letters available, and this leads to a new opportunity in golfing "certain ascii modulo 256"!

Since the existing literals are at most 4 bytes long, we only need to check the combinations of `<letter><digit>` and `<letter><digit><digit>`. I wrote a quick brute-forcer for it, and it turns out that we can save whopping 28 bytes in total.
> 3 votes

# Answer
# Whitespace, score 35, 498 bytes

Uses only `\n`, `\t`, and , which are all allowed:

```
6

```

```
7

```

```
8

```

```
9

```

```
0

```

```
-

```

```
=

```

```
y

```

```
u

```

```
i

```

```
o

```

```
p

```

```
[

```

```
]

```

```
h

```

```
j

```

```
k

```

```
l

```

```
;

```

```
'

```

```
\

```

```
v

```

```
b

```

```
n

```

```
m

```

```
^

```

```
&

```

```
*

```

```
(

```

```
)

```

```
_

```

```
+

```

```
:

```

```
"

```

```
|

```

## Explanation

* For `67890`, the number is pushed to the stack (`SS` *number* `N`) and printed as a number (`TNST`).
* For everything else, the ASCII code of the character is pushed to the stack (`SS` *number* `N`) and printed as a character (`TNSS`).

Try it all online!
> 2 votes

# Answer
# Husk, score 35, 198 bytes

```
6 D3
7 >1D4
8 D4
9 >1D5
0 Dc1
- ;c45
= ;c>1D31
y ;c121
u ;c<121 4
i ;c>1D53
o ;c111
p ;c112
[ ;c>11D51
] ;c>11D52
h ;cD52
j ;cD53
k ;c>1D54
l ;cD54
; ;c>3D31
' ttsc32
\ ;cDD23
v ;c<121 3
b ;c>4D51
n ;cD55
m ;c>1D55
^ ;c<D51D4
& ;c<41 3
* ;c42
( ;c<41 1
) ;c41
_ ;c>11D53
+ ;c43
: ;c>4D31
" ;c34
| ;c124

```

The additional character is `;`; it is equivalent to `R1`. `<` and `>` yield the absolute difference between their arguments if the comparison is successful. The only real nontrivial programs here are `Dc1` and `ttsc32`.
> 2 votes

# Answer
## Groovy, score 35/35, ~~397~~ 380 bytes

Fairy character: "h". Each character is a one-statement program.

```
/* 6 */ 3<<1
/* 7 */ 22%15
/* 8 */ 1<<3
/* 9 */ 24%15
/* 0 */ 1%1
/* - */ 45as char
/* = */ 122>>1as char
/* y */ 121as char
/* u */ 234>>1as char
/* i */ 211>>1as char
/* o */ 111as char
/* p */ 112as char
/* [ */ 212%121as char
/* ] */ 214%121as char
/* h */ 13<<3as char
/* j */ 53<<1as char
/* k */ 214>>1as char
/* l */ 54<<1as char
/* ; */ 211%152as char
/* ' */ 121%41as char
/* \ */ 23<<2as char
/* v */ 241%123as char
/* b */ 211%113as char
/* n */ 55<<1as char
/* m */ 221%112as char
/* ^ */ 215%121as char
/* & */ 122%42as char
/* * */ 42as char
/* ( */ 5<<3as char
/* ) */ 41as char
/* _ */ 321%113as char
/* + */ 43as char
/* : */ 232>>2as char
/* " */ 34as char
/* | */ 124as char

```
> 2 votes

# Answer
# Numberwang, score 35, 1169 bytes

```
6 65656565656565656565643
7 656565656565656565656433
8 6565656565656565656565613
9 6565656565656565656565623
0 656565656565656565633
- 6565656565656565656333
= 656565656565656565656565613
y 656565656565656565656565656565656565656565656565613
u 6565656565656565656565656565656565656565656565623
i 65656565656565656565656565656565656565656565656233
o 65656565656565656565656565656565656565656565613
p 65656565656565656565656565656565656565656565623
[ 656565656565656565656565656565656565613
] 656565656565656565656565656565656565633
h 6565656565656565656565656565656565656565643
j 656565656565656565656565656565656565656565613
k 656565656565656565656565656565656565656565623
l 656565656565656565656565656565656565656565633
; 6565656565656565656565643
' 65656565656565643
\ 656565656565656565656565656565656565623
v 6565656565656565656565656565656565656565656565633
b 65656565656565656565656565656565656565633
n 656565656565656565656565656565656565656333
m 656565656565656565656565656565656565656565643
^ 656565656565656565656565656565656565643
& 65656565656565633
* 6565656565656565623
( 65656565656565656233
) 6565656565656565613
_ 65656565656565656133
+ 6565656565656565633
: 6565656565656565656565633
" 656565656565643
| 656565656565656565656565656565656565656565656565643

```

Numberwang is a language based on Brainf\**k where all commands consist of single numbers.

From the original character set we could use:
`1` \- decrement the pointer (to point to the next cell to the left)
`2` \- decrement (decrease by one) the byte at the pointer
`3` \- output the value of the byte at the pointer
~~`4` \- start of loop, needs corresponding 7. Skips loop if active pointer equals 0~~ Useless without a 7
`5` \- directly decrements the current pointer with the following number

Using only the allowed characters we could get a 2572 bytes solution by keeping decrementing by 5 with `55` and then subtracting the remainder of the division \\$(256-charcode)/5\\$.

But luckily the fairy comes in and gives us:
`6` \- directly increments the current pointer with the following number

With this new command and the fact that the password is composed of only ASCII characters (\<127) we can add up from 0 (which is clearly better then subtracting from 256) in steps of 5 with `65`, and then add the remainder of \\$(charcode)/5\\$.
> 2 votes

# Answer
# 05AB1E, score 35, ~~162~~ 149 bytes

Uses the characters `!12345<>RTxç`.

```
6 3!
7 3!>
8 4x
9 T<
0 1<
- 45ç
= 15>Rç
y 121ç
u 115>>ç
i 52x>ç
o 111ç
p 112ç
[ 45x>ç
] 45>x>ç
h 52xç
j 53xç
k 54x<ç
l 54xç
; 31<x<ç
' Txx<ç
\ 23xxç
v 5!<<ç
b 25x<xç
n 55xç
m 54x>ç
^ 24x<xç
& Tx<xç
* 42ç
( Txxç
) 41ç
_ 24xx<ç
+ 43ç
: 15x<xç
" 34ç
| 124ç

```

Try it online!

`&` can be printed with `CCxç`, but this doesn't work with my test suite :/.
> 2 votes

# Answer
# Fission 2, score 35, 371 bytes

```
6 R'5@$@!A
7 R'5@$$@!A
8 R'5@$$$@!A
9 R'5@$$$$@!A
0 R'%@$$$$$$$$$$$@!A
- R'%@$$$$$$$$@!A
= R'<@$@!A
y Rx@$@!A
u Rt@$@!A
i Rg@$$@!A
o Rg@$$$$$$$$@!A
p Rg@$$$$$$$$$@!A
[ R'Z@$@!A
] R'Z@$$$@!A
h Rg@$@!A
j Rg@$$$@!A
k Rg@$$$$@!A
l Rg@$$$$$@!A
; R'5@$$$$$$@!A
' R'%@$$@!A
\ R'Z@$$@!A
v Rt@$$@!A
b Ra@$@!A
n Rg@$$$$$$$@!A
m Rg@$$$$$$@!A
^ R'Z@$$$$@!A
& R'%@$@!A
* R'%@$$$$$@!A
( R'%@$$$@!A
) R'%@$$$$@!A
_ R'Z@$$$$$@!A
+ R'%@$$$$$$@!A
: R'5@$$$$$@!A
\ R'Z@$$@!A
" R'!@$@!A
| R'z@$$@!A

```

Thank god there was some letter in there that could destroy atoms.
> 1 votes

# Answer
# Actually, 35 characters (18 complete), ??? bytes

The only interesting ones are:

```
6 3!
7 3!FD
8 3!F
9 3!FDFDDDD
0 1D
- 4f$F

```

The others are just "make some number with `F` and `D` and then change it to a char with `c`".

```
= 3!FDFDDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
y 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDc
u 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDc
i 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
o 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
p 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
[ 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
] 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
h 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
j 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
k 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc
l 3!FDFDFDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDc

```
> 1 votes

# Answer
# asdf, 35 chars, 1420 bytes

```
6 asaasaaaasssasaaaasssasaasssasaaaasssasaa
7 asaasaaaasssasaaaasssasaasssasaaaaaa
8 asaasaaaasssasaaaaaasssasaaaaaa
9 asaasaaaasssasaaaaaasssasaaaasssasaa
0 asaasaaaasssasaaaasssasaaaaaaaa
- asaasaaaasssasaasssasaasssasaaaasssasaasssasaa
= asaasaaaasssasaaaaaaaasssasaasssasaa
y asaasaasssasaaaaaaaasssasaaaasssasaa
u asaasaasssasaaaaaasssasaasssasaasssasaasssasaa
i asaasaasssasaaaasssasaasssasaasssasaaaasssasaa
o asaasaasssasaaaasssasaasssasaaaaaaaa
p asaasaasssasaaaaaasssasaaaaaaaa
[ asaasaasssasaasssasaasssasaaaasssasaasssasaaaa
] asaasaasssasaasssasaasssasaaaaaasssasaasssasaa
h asaasaasssasaaaasssasaasssasaasssasaaaaaa
j asaasaasssasaaaasssasaasssasaasssasaasssasaasssasaa
k asaasaasssasaaaasssasaasssasaasssasaasssasaaaa
l asaasaasssasaaaasssasaasssasaaaasssasaaaa
; asaasaaaasssasaaaaaasssasaasssasaaaa
' asaasaaaasssasaasssasaaaasssasaaaaaa
\ asaasaasssasaasssasaasssasaaaaaasssasaaaa
v asaasaasssasaaaaaasssasaasssasaaaasssasaa
b asaasaasssasaaaasssasaaaaaasssasaasssasaa
n asaasaasssasaaaasssasaasssasaaaaaasssasaa
m asaasaasssasaaaasssasaasssasaaaasssasaasssasaa
^ asaasaasssasaasssasaasssasaaaaaaaasssasaa
& asaasaaaasssasaasssasaaaasssasaaaasssasaa
* asaasaaaasssasaasssasaasssasaasssasaasssasaasssasaa
( asaasaaaasssasaasssasaasssasaasssasaaaaaa
) asaasaaaasssasaasssasaasssasaasssasaaaasssasaa
_ asaasaasssasaasssasaasssasaaaaaaaaaa
+ asaasaaaasssasaasssasaasssasaasssasaasssasaaaa
: asaasaaaasssasaaaaaasssasaasssasaasssasaa
" asaasaaaasssasaasssasaaaaaasssasaasssasaa
| asaasaasssasaaaaaaaaaasssasaaaa

```

Accidentally find out some working languages... I have no idea what it is. Anyway, one program per each line. 2 chars at beginning of each line is not a part of the program.
> 0 votes

---
Tags: string, code-challenge, restricted-source
---


title: How hyperperfect am I?

text:
# Question
Title: How hyperperfect am I?
A \\$k\\$-hyperperfect number is a natural number \\$n \ge 1\\$ such that

$$n = 1 + k(\sigma(n) − n − 1)$$

where \\$\sigma(n)\\$ is the sum of the divisors of \\$n\\$. Note that \\$\sigma(n) - n\\$ is the proper divisor sum of \\$n\\$. The sequence of \\$k\\$-hyperperfect numbers begins

$$6, 21, 28, 301, 325, 496, 697, \dots$$

This is A034897 on the OEIS.

For example:

$$\begin{align} \sigma(21) & = 1 + 3 + 7 + 21 = 32 \\ 21 & = 1 + 2(32 - 21 - 1) \\ & = 1 + 2 \times 10 \\ & = 21 \end{align}$$

Therefore, \\$21\\$ is a \\$2\\$-hyperperfect number.

---

You are to take a \\$k\\$-hyperperfect number \\$n\\$ as input and output the value of \\$k\\$. You can assume you will never have to handle numbers greater than your language's limit at any point (i.e. \\$k\sigma(n)\\$ will always be within the bounds of your language), and you may input and output in any convenient format.

The input is guaranteed to be a \\$k\\$-hyperperfect number, you don't have to handle inputs such as \\$2, 87, 104\\$ etc. that aren't \\$k\\$-hyperperfect.

This is , so the shortest code in bytes wins.

## Test cases

These are the outputs for all the listed values in the OEIS for this sequence, and are the exhaustive list of inputs for \\$n \< 1055834\\$

```
      n       k
      6       1
     21       2
     28       1
    301       6
    325       3
    496       1
    697      12
   1333      18
   1909      18
   2041      12
   2133       2
   3901      30
   8128       1
  10693      11
  16513       6
  19521       2
  24601      60
  26977      48
  51301      19
  96361     132
 130153     132
 159841      10
 163201     192
 176661       2
 214273      31
 250321     168
 275833     108
 296341      66
 306181      35
 389593     252
 486877      78
 495529     132
 542413     342
 808861     366
1005421     390
1005649     168
1055833     348

```

# Answer
# Jelly, 5 bytes

```
:Æṣ’$

```

```
:       integer divide
Æṣ’$    the decremented divisor sum

```

This uses \\$\lfloor\frac{n}{\sigma(n)-n-1}\rfloor\\$ instead of \\$\frac{n-1}{\sigma(n)-n-1}\\$, but it still works because \\$\frac{1}{\sigma(n)-n-1}\\$ can never be greater than or equal to 1.

Try it online!
> 13 votes

# Answer
# , 24 23 20 bytes

```
<.@%1<:@#.[:I.0=i.|]

```

Try it online!

-3 thanks to rak's rounding down trick

Let `n` be the input.

* `<.@%` round down n divided by
* `1<:@#.` 1 minus the sum of
* `[:I.` the indexes where
* `0=` 0 is equal to the remainder when
* `i.` the list 0..n-1
* `|` is divided elementwise into
* `]` n.
> 8 votes

# Answer
# JavaScript (ES6), 36 bytes

```
n=>~-n/(g=k=>~k--&&!(n%k)*k+g(k))(n)

```

Try it online!

### How?

\\$\sigma(n) − n − 1\\$ is the sum of the divisors of \\$n\\$ in \\$\[2..n-1\]\\$. This is also the sum of the divisors of \\$n\\$ in \\$\[-1..n-1\]\\$ because \\$1\\$ and \\$-1\\$ cancel each other out. The helper function \\$g\\$ computes the latter, so that we can use the slightly more golf-friendly halt condition `~k--`.

**Note**: From a mathematical perspective, we really should remove \\$0\\$ from the list of possible divisors. But it is quietly ignored in this code as we get `!NaN*0`, which is \\$0\\$.
> 7 votes

# Answer
# Wolfram Language (Mathematica), 25 bytes

```
--+#/(Tr@Divisors@#-#-1)&

```

Try it online!

-1 byte from @att
> 5 votes

# Answer
# Haskell, 38 bytes

```
k n=div n$sum[d|d<-[2..n-1],mod n d<1]

```

Try it online!
> 5 votes

# Answer
# Japt, ~~12~~ 6 bytes

```
zUâ ¤x

```

Try it
> 5 votes

# Answer
# V (vim), ~~81 79~~ 70 bytes

```
"aD@ai0
<esc>V{g<c-a>{dd}dkqqC<c-r>=!(<c-r>a%<c-r>")*<c-r>"
<esc>k@qq@q:%s/\n/+
$x0C<c-r>=<c-r>a/(<c-r>")

```

Try it online!

Uses the observation in rak1507's Jelly answer.

-2 bytes from kops.

-9 more bytes from kops, removing the entire third line!
> 5 votes

# Answer
# 05AB1E, 5 bytes

*Ties rak1507's Jelly answer for #1.*

```
Ñ¨O<÷

```

Try it online!

```
Ñ¨O<÷  # full program
       # implicit input...
    ÷  # divided by...
  O    # sum of...
Ñ      # divisors of...
       # implicit input...
 ¨     # excluding the last...
    ÷  # rounded down
       # implicit output

```
> 4 votes

# Answer
# C (gcc), ~~45~~ 44 bytes

```
i,t;k(x){for(t=i=1;++t<x;)i-=x%t?0:t;x/=-i;}

```

Try it online!

* uses Jelly's formula
* saved 1 by sum negatively and starting from 1

Explanation

```
   i and t used to get divisors sum
   for(t=i=1;++t<x;) - we start our loop with t=1 and end before x to exclude them from dividers
   i-=x%t?0:t;       - we iterate all values and add to i negatively if modulo t is 0
   x/=-i;           - finally we return trough eax trick x divided by -sum which started from 1 so we have the +1 term added to k(sum)

```
> 4 votes

# Answer
# , 35 bytes

```
function(x,y=x-1)y/sum(2:y*!x%%2:y)

```

Try it online!

Inspired by @Dominic's answer to linked challenge.

Uses the fact that \\$\sigma(n)-n-1\\$ for `n>2` is just sum of divisors that lie between `2` and `n-1`. Then, we simply divide `n-1` by the obtained value to get `k` (as `n` is guaranteed to be hyperperfect).

### , 34 bytes

```
function(x)x%/%sum(2:x*!x%%2:x,-x)

```

Try it online!

Using @rak's formula.
> 4 votes

# Answer
# Retina 0.8.2, 54 bytes

```
.+
$*
1(?=(1(?<=(?=(?(\3+$)(\2?\3)))(1+)))+1)(\2)+
$#4

```

Try it online! Link includes faster test cases. Explanation:

```
.+
$*

```

Convert the input to unary.

```
1(?=(1(?<=(?=(?(\3+$)(\2?\3)))(1+)))+1)(\2)+
$#4

```

Based on @Deadcode's answer to Am I not good enough for you? this calculates the sum of divisors from `2` to `n-1`, but golfed down (removed a `^` and two `$`s) by assuming that the input is a hyperperfect number. After the sum of divisors is calculated the result is then divided into `n-1`.
> 3 votes

# Answer
# Python 3, ~~46~~ 45 bytes

1 byte off thanks to @dingledooper! Also, this uses the floor trick from @rak1507's answer.

```
lambda n:n//sum(k*(n%k<1)for k in range(2,n))

```

Try it online!
> 2 votes

# Answer
# K (oK), 18 bytes

```
{_x%+/1_&~(!x)!'x}

```

Try it online!

A port of @Jonah's J answer \- don't forget to upvote him!
> 2 votes

# Answer
# Vyxal, 4 bytes

```
∆K‹ḭ

```

Try it Online!

Porting the jelly answer ftw

## Explained

```
∆K‹ḭ
∆K   # sum of proper divisors of input
  ‹  # ↑ - 1
   ḭ # input // ↑ (integer division)

```
> 1 votes

# Answer
# Excel, 52 bytes

```
=LET(n,A2-1,q,SEQUENCE(n),n/SUM((MOD(A2,q)=0)*q,-1))

```

Works except for the last test case. `SEQUENCE` is limited to 2^20. The following works up to 2^40 and is 79 bytes.

```
=LET(q,SEQUENCE(A2^0.5),a,(MOD(A2,q)=0)*q,(A2-1)/(SUM(a,IFERROR(A2/a,0))-A2-1))

```
> 1 votes

# Answer
# Pari/GP, 23 bytes

```
n\sum(i=2,n-1,!(n%i)*i)

```
> 1 votes

---
Tags: code-golf, math, number-theory
---


title: Check B-powersmoothness

text:
# Question
Title: Check B-powersmoothness
from Wikipedia, a number is called B-powersmooth if all prime powers \\$p^v\\$ that divide the number satisfy \\$p^v \leq B\\$. B-powersmoothness is important, for example, for Pollard's p-1 factorization algorithm.

## Task

your task is to get two numbers, \\$n\\$ and \\$B\\$, and output if \\$n\\$ is \\$B\\$-powersmooth.

## Rules

* You may take the input in any reasonable format
* Since this is , the shortest code wins.
* You can output a truthy/false value, or any two different values for true/false.
* You can assume \\$ 1 \< B, n \\$.

## Test cases

```
n, b -> answer
3, 2 -> 0
10, 7 -> 1
123, 40 -> 0
123, 41 -> 1
64, 2 -> 0
64, 32 -> 0
64, 65 -> 1
720, 5 -> 0
720, 15 -> 0
720, 16 -> 1
20, 5 -> 1

```

# Answer
> 9 votes
# , ~~14~~ 13 bytes

```
<[:>./2^/@p:]

```

Try it online!

*-1 byte thanks to @Jonah*

A dyadic function. The right argument is \\$n\\$, the left argument is \\$B\\$. Returns 0 if \\$n\\$ is \\$B\\$-powersmooth, 1 otherwise.

### How it works

```
<[:>./2^/@p:]  NB. left arg = B, right arg = n
      2   p:]  NB. Prime factorization of n,
               NB. given as a 2-row matrix of [primes, exponents]
       ^/@     NB. Evaluate all pi^ei for each prime factor
 [:>./         NB. Maximum of all prime powers found above
<              NB. 1 if the above is greater than B, 0 otherwise

```

# Answer
> 8 votes
# Regex (ECMAScript), 61 bytes

```
^(?=(|x*)\1*(?=\1\b)(?!(((x+)x+)(?=\3+,)\3*\4)\2+,)(x*)).*,\5

```

Try it online!

Takes \\$n\\$ and \\$B\\$ from input in unary as strings of `x`s whose length represents the number, separated by a `,`.

Uses my prime power regex, and the same expression for cycling through divisors of N from largest to smallest as used in this problem.

```
# Take input as N in unary, followed by a comma, followed by B in unary
^
(?=
    (|x*)\1*(?=\1\b)  # cycle tail through all of the divisors of N, including N,
                      # from largest to smallest;
                      # \1 = the divisor, or zero if the divisor is N itself
    # Assert tail is a prime power
    (?!               # Assert that the following cannot match
        (             # Capture \2 to be the following:
            ((x+)x+)  # Cycle through all values of \4 and \3 such that \3 > \4 > 1
            (?=\3+,)  # such that \3 is a proper divisor of N
            \3*\4     # Cycle through all values of \2 > \3 that aren't divisible
                      # by \3, by letting \2 = \3 * A + \4 where A >= 0
        )
        \2+,          # where \2 is a proper divisor of N
    )
    (x*)              # \5 = the largest prime power factor of N
)
.*,                   # tail = B
\5                    # Assert that \5 <= B

```

# Regex (ECMAScript), ~~61~~ 59 bytes

Adapting Neil's idea of taking the arguments in \\$(B,n)\\$ order saves 2 bytes:

```
^(x*),(?!(x*)\2*(?=\2\b)(?!(((x+)x+)(?=\4+$)\4*\5)\3+$)x\1)

```

Try it online!
(For convenience, this test harness takes the arguments in \\$(n,B)\\$ order and passes them to the regex in reverse order.)

```
# Take input as B in unary, followed by a comma, followed by N in unary
^
(x*),                 # \1 = B; tail = N
(?!                   # Assert that the following cannot match
    (x*)\2*(?=\2\b)   # cycle tail through all of the divisors of N, including N;
                      # \2 = the divisor, or zero if the divisor is N itself
    # Assert tail is a prime power
    (?!               # Assert that the following cannot match
        (             # Capture \3 to be the following:
            ((x+)x+)  # Cycle through all values of \5 and \4 such that \4 > \5 > 1
            (?=\4+$)  # such that \4 is a proper divisor of N
            \4*\5     # Cycle through all values of \3 > \4 that aren't divisible
                      # by \4, by letting \3 = \4 * A + \5 where A >= 0
        )
        \3+$          # where \3 is a proper divisor of N
    )
    x\1               # Assert tail > B
)

```

# Regex (ECMAScript 2018 / .NET), ~~54~~ 52 bytes

*-2 bytes by using Neil's idea but with the arguments in \\$(n,B)\\$ order*

```
$(?<!^\4*(?!(((x+)x+)(?=\2+,)\2*\3)\1+,)(x+\5),(x+))

```

Try it online! \- ECMAScript 2018
Try it online! \- .NET

This works by first capturing \\$B\\$, then asserting that there does not exist any \\$a\>B\\$ for which \\$a\\$ is a prime power that divides \\$N\\$.

```
# Take input as N in unary, followed by a comma, followed by B in unary
$                     # head = B
(?<!                  # Negative lookbehind; assert that it's not possible to find a
                      # match for the following. Evaluated from right-to-left, so read
                      # the inside of this from bottom to top (but go back to top-down
                      # order for reading inside the negative lookbehind)
    ^\4*              # Assert that \4 divides N
    # Assert tail is a prime power
    (?!               # Negative lookahead; assert that the following cannot match
        (             # Capture \1 to be the following:
            ((x+)x+)  # Cycle through all values of \3 and \2 such that \2 > \3 > 1
            (?=\2+,)  # such that \2 is a proper divisor of N
            \2*\3     # Cycle through all values of \1 > \2 that aren't divisible
                      # by \2, by letting \1 = \2 * A + \3 where A >= 0
        )
        \1+,          # where \2 is a proper divisor of N
    )
    (x+\5)            # \4 = tail = any divisor of N greater than B; the "^\5*" above
                      #      asserts it to be a divisor of N. Note that in most regex
                      #      engines, putting the "^\5*" later than the negative
                      #      assertion, as is the case here, results in a slowdown, as
                      #      non-divisors of N will be tested for being prime powers.
    ,(x+)             # \5 = head = B
)

```

# Answer
> 5 votes
# MATL, 6 bytes

```
&YF^<a

```

Inputs `n` then `B`.

Outputs `0` if *n* is *B*-powersmooth or `1` otherwise.

Try it online! Or verify all test cases

### How it works

```
&YF  % Implicit input: n. Factorization with primes and exponents as separate outputs
^    % Element-wise power
<    % Implicit input: B. Less-than comparison, element-wise
a    % Any: gives true (1) if any value is nonzero, or false (0) otherwise
     % Implicit display

```

# Answer
> 5 votes
# Brachylog, 5 bytes

```
ḋḅ∋×>

```

Try it online!

Takes \\$n\\$ through the input variable and \\$B\\$ through the output variable; fails if \\$n\\$ is \\$B\\$-powersmooth and succeeds if not.

```
 ḅ       Take the runs of consecutive equal elements from
ḋ        the prime factors of n.
  ∋      For some run,
   ×     the product of its elements
    >    is greater than B.

```

# Answer
> 4 votes
# Factor \+ `math.primes.factors`, 37 bytes

```
[ group-factors unzip v^ supremum < ]

```

Try it online!

## Explanation:

It's a quotation (anonymous function) that takes `B` and `n` (in that order) as input and returns a (flipped) boolean as output.

* `group-factors` obtain the prime factorization of `n` as a sequence of pairs
* `unzip` get the factors in one sequence and the exponents in another
* `v^` element-wise exponentiation of two vectors
* `supremum` find the maximum value
* `<` is `B` less than this value?

# Answer
> 4 votes
# Husk, ~~8~~ 7 bytes

```
≤⁰▲mΠgp

```

Try it online!

```
      p        # get the list of prime factors of arg2;
     g         # group equal ones together
   mΠ          # and multiply them together (to get the prime power divisors);
  ▲            # get the maximum of these;
≤⁰             # is it ≤ arg1?

```

# Answer
> 3 votes
# Jelly, 7 bytes

```
ÆF*/€Ṁ>

```

Try it online!

Similar to Bubbler's answer. Returns a flipped boolean.

## Explanation

```
ÆF*/€Ṁ< Main link: takes  n on the left, and B on the right
ÆF      prime factor and exponent pairs
  */€   reduce each by exponentiation
     Ṁ  maximum
      > greater than B?

```

# Answer
> 3 votes
# Python 2, 68 bytes

The output is a positive integer for truthy cases and a `0` otherwise.

```
f=lambda n,b,p=2,m=1:b/m*(n<2or f(*[n/p,n,b,b,p,p+1,m*p][n%p>0::2]))

```

Try it online!

**Commented:**

```
f=lambda n,b,p=2,m=1:         # recursive function taking 4 arguments
                              # n, b - inputs from the challenge
                              # p=2  - candidate for a prime dividing n
                              # m=1  - a power of p dividing the original input n
b/m*( ... )                   # floor division, this is 0 if m>b
     n<2or ...                # if n==1, return True (1), else:
           f(*[ ... ][1::2])  # if n%p>0 (p does not divide n):
        == f(n,b,p+1)         #   try next p
           f(*[ ... ][0::2])  # if n%p==0 (p divides n):
        == f(n/p,b,p,m*p)     #   divide n by p, update power of p, m

```

# Answer
> 3 votes
# Haskell, ~~108~~ 107 bytes

```
1!_=[]
n!f|n`mod`f<1=f:(n`div`f)!f|x<-f+1=n!x
g(h:t)x|h`mod`x<1=h*x:t|1>0=x:h:t
n#b=all(<=b)$foldl g[1]$n!2

```

Try it online!

* saved 1 thanks to @Unrelated String
* `!` finds all prime factors : it divides n by 2 while modulo is 0 then by 3, then by 4.. Wait 4 is not prime! Ah but it was already factorized by 2
* `g` all that to group factors
* `#` finally we return True if all are \<= B

# Answer
> 2 votes
# Retina 0.8.2, ~~63~~ ~~59~~ 58 bytes

```
.+
$*
Mr`^\4*(?!(((1+)1+)(?=\2+¶)\2*\3)\1+¶)(1+\5)¶(1+)$
0

```

Try it online! Takes line-separated input `n,B` but test suite converts from comma-separated for convenience. Edit: Saved 1 byte thanks to a hint by @Deadcode. Explanation:

```
.+
$*

```

Convert `n` and `B` to unary.

```
Mr`^\4*(?!(((1+)1+)(?=\2+¶)\2*\3)\1+¶)(1+\5)¶(1+)$

```

Try to find (in `$4`) an integer that is greater than `b`, a power of a prime, and a factor of `n`. The `r` flag makes the expression match from the right, so `$5` is matched to `b` first, then `$4`, then `$4` is checked for the power of a prime (taken from @Deadcode's answer to Is it almost-prime?, matching left-to-right as it is a lookahead), then it is checked for a factor of `n`. The previous 63-byte version was faster because it checked for factors before prime powers: Try it online!

```
0

```

Check that there wasn't a match.

# Answer
> 2 votes
# JavaScript (ES6),  ~~53~~  52 bytes

Expects `(b)(n)`. Returns **0** or **true**.

```
b=>g=(n,i=d=1)=>i>b?0:n%++d?d>n||g(n,1):g(n/d,i*d--)

```

Try it online!

### Commented

```
b =>          // outer function taking b = powersmoothness rank
g = (         // recursive inner function taking:
  n,          //   n = input integer whose b-powersmoothness is tested
  i =         //   i = exponentiation of the current prime divisor,
              //       which must never exceed b
  d = 1       //   d = prime divisor candidate
) =>          //
i > b ?       // if i is greater than b:
  0           //   failure: stop and return 0
:             // else:
  n % ++d ?   //   increment d; if it's not a divisor of n:
    d > n     //     stop and return true if it's greater than n
    ||        //     otherwise:
      g(      //       do a recursive call:
        n,    //         pass n unchanged
        1     //         reset i to 1 while leaving d unchanged
      )       //       end of recursive call
  :           //   else (d is a prime divisor of n):
    g(        //     do a recursive call:
      n / d,  //       divide n by d
      i * d-- //       multiply i by d, decrement d afterwards
    )         //     end of recursive call

```

# Answer
> 2 votes
# 05AB1E, 5 bytes

*Ties Unrelated String's Brachylog answer for #1.*

```
ÒγP@P

```

Try it online!

```
ÒγP@P  # full program
    P  # product of...
       # (implicit) list of...
   @   # whether...
       # implicit input...
   @   # is greater than or equal to...
  P    # products of...
 γ     # consecutive runs of...
Ò      # prime factors of...
       # implicit input
       # implicit output

```

# Answer
> 2 votes
# C (gcc), ~~66~~ 64 bytes

```
t;r;p;f(n,B){for(r=p=1;n/++p;r&=t/n<=B)for(t=n;n%p<1;n/=p);t=r;}

```

Try it online!

Takes two integers \\$n\\$ and \\$B\\$ as input and returns \\$1\\$ if \\$n\\$ is \\$B\\$-powersmooth or \\$0\\$ otherwise.

### Explanation

```
t;r;p;f(n,B){                           // function taking two int inputs
                                        // n and B
             for(r=p=1;                 // loop over prime factors p of n
                                        // also init return value r to 1
                       n/++p;           // loop while p<=n also bump p
                             r&=t/n<=B) // at the end of each loop check that
                                        // the current prime power factor of
                                        // n (t/n) is less than or equal to B
                  for(                  // loop to strip n of all its
                                        // prime factors p
                      t=n;              // save copy of n in t before
                                        // removing all p factors
                          n%p<1;        // loop while p is a factor of n
                                n/=p);  // reduce n by p
             t=r;                       // return r
}

```

# Answer
> 1 votes
# Pyth, ~~24~~ 23 bytes

```
FkrPE8 aY^@k1@k0;<eSYhE

```

Try it online!

# Explanation

```
Fk    For loop with the iterator as k
E     Take first input.
PE    List of prime factors
rPE8  Run length encoding over the list, eg: [2, 2, 3] becomes [[2, 2], [1, 3]]
Y     Initialized to an empty list by default.
@k1   Element at index 1 of k.
@k0   Element at index 0 of k.
^     Apply exponentiation.
aY    Append element to Y
SY    Sort Y
e     Last element
hE    Take next input and increment by 1
<     Is it less than?

```

# Answer
> 1 votes
# Ruby, 48 bytes

```
->n,b{(2..n).none?{|x|n/(n/=x while n%x<1;n)>b}}

```

Try it online!

# Answer
> 1 votes
# JavaScript (Node.js), 76 bytes

```
n=>g=b=>{for(++b,p=1;b%++p;);for(q=b;q%p<1;q/=p);return n%b|q>1&&b<n&&g(b);}

```

Try it online!

Return `false` if n is B-powersmooth. Return `0` otherwise. (as per two different values rule)

# Answer
> 1 votes
# Charcoal, 37 bytes

```
ＮθＷ¬⁼θΠυ⊞υ§Φ…·²θ¬﹪÷θ∨Πυ¹κ⁰¬›⌈ＥυＸι№υιＮ

```

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. `-` for smooth, nothing if not. Explanation:

```
ＮθＷ¬⁼θΠυ⊞υ§Φ…·²θ¬﹪÷θ∨Πυ¹κ⁰

```

Input `n` and find its prime factors (taken from my answer to Zeroes at end of \\$n!\\$ in base \\$m\\$).

```
¬›⌈ＥυＸι№υιＮ

```

Raise each prime factor to its multiplicity and compare the largest to `B`.

# Answer
> 1 votes
# Excel, 149 bytes

```
=LET(q,SEQUENCE(A2),f,(MOD(A2,q)=0)*q,o,(MMULT(1*(MOD(f,TRANSPOSE(q))=0),q^0)=2)*f,p,FILTER(o,o),MAX((MMULT(1*(MOD(f,TRANSPOSE(p))=0),p^0)=1)*f)<=B2)

```

Explanation

```
=LET(                                        'function that allows variable assignment,
                                                 returns the value of the last parameter
q,SEQUENCE(A2)                               'q = list of numbers from 1 to n
f,(MOD(A2,q)=0)*q                            'f = if n mod q = 0 then q else 0
o,(MMULT(1*(MOD(f,TRANSPOSE(q))=0),q^0)=2)*f 'o = if f is prime then f else 0
         1*(MOD(f,TRANSPOSE(q))=0)           'matrix of f by q where 1 indicates f mod q = 0
                                   q^0       'vertical matrix of q 1's
   MMULT(                             )      'need to use matrix multiplication to sum
                                                 each row inside a LET
                                       =2)*f 'if sum of the row is 2 (prime) then f else 0
p,FILTER(o,o)                                'p = all o where o <> 0
        1*(MOD(f,TRANSPOSE(p))=0)            'matrix of f by p where 1 indicates f mod p = 0
MAX((MMULT(                   ,p^0)=1*f)<=b2 'TRUE if largest f with one prime factor <= b

```

Spreadsheet

# Answer
> 1 votes
# Orst, 18 bytes

```
κñˋςᏓ{ᏓĖΐÇ}Ñδ

```

Try it online!

As hex bytes, this is

```
DE 87 FF D0 E7 FF 47 FF A0 FF 47 18 DD 0E FF A5 2F D4

```

Encoding as UTF-8:

```
Þ‡ÿÐçÿGÿ ÿGÝÿ¥/Ô
```

## How it works

```
κñˋςᏓ{ᏓĖΐÇ}Ñδ - Full program. Takes b, then a
κ             - Save b in variable $x
 ñˋ           - Remove and swap. Stack is [a]
   ςᏓ         - Base-and-powers; Prime decomposition of a
     {    }Ñ  - Over each pair:
      ᏓĖ      -   Reduce by power
        ΐ     -   Retrieve b
         Ç    -   Less than or equal?
            δ - True for all?

```

---
Tags: code-golf, math, decision-problem
---


title: You are kinda Replacable to Me

text:
# Question
Title: You are kinda Replacable to Me
You are provided with a non-empty array \\$A\\$ of integers, all greater than 0. But what good is an array if the elements do not sum up to the number \\$N\\$ (also provided as input)...

So to change that, you can pick any integer of your choice present in the array and replace any of its digits with a different digit between 0-9 of your choice. What is the **minimum** number of digits that you should replace so that the sum of the elements becomes \\$N\\$?

Note that after replacing the digits, some of the numbers may contain leading zeroes, which is fine. For example: you can change \\$123\\$ to \\$023\\$, or even \\$000\\$ if you want to. However, the input will not contain any leading zeroes and you cannot assume any before the input number.

# Example

Consider the array \\$A=\[195, 95\]\\$ and \\$N = 380\\$. You can do the following replacements:

1. 1**9**5 -\> 1**8**5
2. 18**5** -\> 18**1**
3. **1**81 -\> **2**81
4. 9**5** -\> 9**9**

So the new array is \\$A=\[281,99\]\\$ with a sum of \\$380\\$ in 4 steps. However, this is not minimal and you can do it in even fewer steps:

1. **1**95 -\> **2**95
2. **9**5 -\> 85

So we got our desired sum in 2 steps. There is no way to get a sum of \\$380\\$ in less than 2 steps so the answer for \\$A=\[195, 95\]\\$ and \\$N = 380\\$ will be \\$2\\$.

# More examples

```
  A, N -> Answer
[195, 95], 380 -> 2
[100, 201], 0 -> 3
[32, 64], 96 -> 0
[7, 1, 1], 19 -> 2
[100, 37], 207 -> 1
[215], 25 -> 2
[123, 456], 1998 -> 6

```

# Rules

* Standard loopholes are forbidden.
* You do not need to handle inputs for which answer does not exist.
* This is , so the shortest code, in bytes, wins.

# Answer
# Haskell, ~~95~~ 93 bytes

```
a#n|s<-show=minimum[sum[1|(x,y)<-s a`zip`s b,x/=y]|b<-mapM(mapM(:s(56^7)))a,n==sum(read<$>b)]

```

Try it online!

The relevant function is `(#)`, which takes as input the array `a` and the integer `n` and returns the minimum number of moves. Elements of `a` are represented as strings of digits.

## How?

Standard bruteforce, we try all the possible substitutions and return the minimum number of changes. The only things I'm vaguely proud of are the `show` tricks I've found while working on this answer.

* First of all, `mapM(:show(56^7))` is shorter than `mapM(\_->['0'..'9'])` for enumerating all the possible digits substitutions of a single number. We save 3 bytes and we gain an exponentially slower solution.
* Moreover, using `show a`zip`show b` instead of `zip(a>>=id)$b>>=id` saves another byte.
* With the two previous tricks, we use the `show` function often enough that defining an alias for it saves an additional byte.
> 5 votes

# Answer
# JavaScript (ES6),  ~~132 123~~  120 bytes

*Saved 3 bytes by wrapping in `eval()`, as suggested by @l4m2*

Expects `(list)(n)`, where `list` is an array of strings.

```
a=>n=>eval('for(m=k=1+a.join``;k--;)m=a.map(x=>s-=x.replace(/./g,j=>(d=~~i%10,t+=d!=j,i/=10,d)),s=n,i=k,t=0)|s|t>m?m:t')

```

Try it online!

### Commented

This is a version without `eval()` for readability.

```
a =>                        // a[] = list of positive integers, as strings
n => {                      // n = target sum
  for(                      // loop:
    m = k =                 //   start with m = k = a number higher than 10 ** p
      1 + a.join``;         //   where p is the total number of digits in a[]
    k--;                    //   stop when k = 0 (decrement it afterwards)
  )                         //
    m =                     //   update the minimum m:
      a.map(x =>            //     for each integer x in a[]:
        s -=                //       subtract from s:
          x.replace(        //         replace in x
            /./g, j => (    //         each digit j:
              d = ~~i % 10, //           extract the next digit d from i
              t += d != j,  //           increment t if d is not equal to j
              i /= 10,      //           divide i by 10
              d             //           replace j with d
            )               //
          ),                //         end of replace
          s = n,            //         start with s = copy of n,
          i = k,            //         i = copy of k
          t = 0             //         and t = 0 (counter of modified digits)
      )                     //     end of map()
      | s                   //     if s is not equal to 0
      | t > m ? m           //     or t is greater than m, leave m unchanged
              : t;          //     otherwise, update it to t
  return m                  // end of loop: return m
}                           //

```
> 4 votes

# Answer
# JavaScript (Node.js), 140 bytes

```
n=>g=x=>x.every(t=>t.split`+`.reduce((a,b)=>a-b,n))&&1+g(x,x.map(t=>{for(i in t)for(j=10;1/t[i]&&j--;)x.push(t.slice(0,i)+j+t.slice(-~i))}))

```

Try it online!

If no expression equals, replace every digit to any another and try again
> 3 votes

# Answer
# Jelly, 20 bytes

Maybe there's a smarter way to approach this for golf?

```
D©⁵ṁḶŒp€ŒpḌS⁼ɗƇn®§§Ṃ

```

A dyadic Link accepting a list of positive integers, \\$A\\$, on the left and an integer, \\$N\\$, on the right that yields the minimal number of digit changes to make to \\$A\\$ that are needed to make it sum to \\$N\\$.

**Try it online!** Or see all but one of the tests or the other one.

### How?

```
D©⁵ṁḶp/€ŒpḌS⁼ɗƇn®§§Ṃ - Link: positive integer array A; integer N
D                    - decimal digit lists (for each of A)
 ©                   - copy that to the register for later (and yield it for now)
  ⁵                  - literal ten
   ṁ                 - mould-like (the digit lists)
    Ḷ                - lowered range (giving us [0,1,...,9] for each digit of each number in A)
       €             - for each (digit-option-list-of-lists):
     Œp              -   Cartesian product (giving us all digit lists we could make for each of A)
        Œp           - Cartesian product (giving us all lists of such possibilities)
              Ƈ      - filter by:
             ɗ       -   last three links as a dyad - f(x=that, N)
          Ḍ          -     convert (each) from base ten
           S         -     sum
            ⁼        -     equals (N)?
                ®    - recall the original digits lists of A from the register
               n     - not equal? (vectorises)
                 §   - sum of each
                  §  - sum of each
                   Ṃ - minimum

```
> 3 votes

# Answer
# Ruby, 154 bytes

```
->a,n{[0].product(*a.map{|c|[*0...10**c.to_s.size]}).select{|x|x.sum==n}.map{|c|(0...a.size).sum{|i|a[i].digits.zip(c[i+1].digits).count{|x,y|x!=y}}}.min}

```

Try it online!

Not particularly clever: for each element of a, checks all numbers between 0 and 10^(number of digits), then filters the substitutions which yields the desired sum, and counts the digits that have changed.
> 2 votes

# Answer
# Charcoal, 38 bytes

```
ＮθＩ⌊ＥΦＥＸχＬη⭆η⎇⁼λ,λ﹪÷ιＸχμχ⁼θΣιΣＥη¬⁼λ§ιμ

```

Try it online! Very slow, so TIO can't handle arrays where the total length of digits and commas is greater than 5. Explanation:

```
Ｎθ                                      First input as a number
        χ                               Predefined variable `10`
       Ｘ                                Raised to power
          η                             Second input as a string
         Ｌ                              Length
      Ｅ                                 Map over implicit range
            η                           Second input as a string
           ⭆                            Map over characters and join
               λ                        Current character
              ⁼                         Equal to
                ,                       Literal string `,`
             ⎇                          If true then
                 λ                      Current character else
                    ι                   Outer value
                   ÷                    Integer divide
                      χ                 Predefined variable `10`
                     Ｘ                  Raised to power
                       μ                Inner index
                  ﹪                     Modulo
                        χ               Predefined variable `10`
     Φ                                  Filter strings where
                            ι           Current string
                           Σ            Sum as array of integers
                         ⁼              Equal to
                          θ             First input as a number
    Ｅ                                   Map over strings
                               η        Second input as a string
                              Ｅ         Map over characters
                                  λ     Current character
                                ¬       Is not
                                 ⁼      Equal to
                                   §ιμ  Character from string
                             Σ          Take the sum
   ⌊                                    Take the minimum
  Ｉ                                     Cast to string
                                        Implicitly print

```
> 2 votes

---
Tags: code-golf, math, number
---


title: Hofstadter Q-sequence

text:
# Question
Title: Hofstadter Q-sequence
# Definition

1. a(1) = 1
2. a(2) = 1
3. a(n) = a(n-a(n-1)) + a(n-a(n-2)) for n \> 2 where n is an integer

# Task

Given positive integer `n`, generate `a(n)`.

# Testcases

```
n  a(n)
1  1
2  1
3  2
4  3
5  3
6  4
7  5
8  5
9  6
10 6
11 6
12 8
13 8
14 8
15 10
16 9
17 10
18 11
19 11
20 12

```

# Reference

# Answer
> 10 votes
# Haskell, ~~35~~ 33 bytes

```
a n|n<3=1|b<-a.(-)n=b(b 1)+b(b 2)

```

Defines a function `a`.

# Answer
> 9 votes
## Retina, ~~84~~ ~~83~~ ~~79~~ 74 bytes

Byte count assumes ISO 8859-1 encoding.

```
.+
$*;1¶1¶
+`;(?=(1)+¶(1)+)(?=(?<-1>(1+)¶)+)(?=(?<-2>(1+)¶)+)
$3$4¶
G3=`
1

```

Try it online! (The first line enables a linefeed-separated test suite.)

I'll have to golf this some more later.

# Answer
> 8 votes
# Julia, 29 bytes

```
!n=n<3||!(n-!~-n)+!(n-!~-~-n)

```

Try it online!

### How it works

We redefine the unary operator `!` for our purposes.

If **n** is **1** or **2**, `n<3` returns *true* and this is our return value.

If **n** larger than **2**, `n<3` returns *false* and the *||* branch gets executed. This is a straightforward implementation of the definition, where `~-n` yields **n - 1** and `~-~-n` yields **n - 2**.

# Answer
> 7 votes
# Sesos, 54 bytes

```
0000000: eefb5b 04f83a a75dc2 36f8d7 cf6dd0 af7b3b 3ef8d7  ..[..:.].6...m..{;>..
0000015: cfed12 f661f0 ae9d83 ee63e6 065df7 ce6183 af7383  ....a.....c..]..a..s.
000002a: 76ef3c 3f6383 7eff9c b9e37f                       v.<?c.~.....

```

Try it online

### Disassembled

```
set numin
set numout
add 1
fwd 1
add 1
fwd 6
get
sub 1
jmp
    jmp
        sub 1
        fwd 1
        add 1
        rwd 1
    jnz
    fwd 1
    sub 1
    rwd 2
    add 2
    jmp
        rwd 4
        jmp
            sub 1
            fwd 3
            add 1
            rwd 3
        jnz
        fwd 4
        jmp
            sub 1
            rwd 3
            add 1
            rwd 1
            add 1
            fwd 4
        jnz
        rwd 3
        jmp
            sub 1
            fwd 3
            add 1
            rwd 3
        jnz
        fwd 4
        add 2
        jmp
            rwd 5
            jmp
                rwd 1
                jmp
                    sub 1
                    fwd 2
                    add 1
                    rwd 2
                jnz
                fwd 1
                jmp
                    sub 1
                    rwd 1
                    add 1
                    fwd 1
                jnz
                rwd 1
                sub 1
            jnz
            fwd 2
            jmp
                sub 1
                rwd 1
                add 1
                rwd 1
                add 1
                fwd 2
            jnz
            fwd 1
            jmp
                rwd 2
                jmp
                    sub 1
                    fwd 1
                    add 1
                    rwd 1
                jnz
                fwd 2
                jmp
                    sub 1
                    rwd 2
                    add 1
                    fwd 2
                jnz
                fwd 1
            jnz
            fwd 3
            sub 1
        jnz
        rwd 2
        jmp
            sub 1
            rwd 3
            add 1
            fwd 3
        jnz
        fwd 1
        sub 1
    jnz
    fwd 2
jnz
rwd 7
put

```

Or in Brainfuck notation:

```
+>+>>>>>>,-[[->+<]>-<<++[<<<<[->>>+<<<]>>>>[-<<<+<+>>>>]<<<[->>>+<<<]>>>>++[<<<<<[<
[->>+<<]>[-<+>]<-]>>[-<+<+>>]>[<<[->+<]>>[-<<+>>]>]>>>-]<<[-<<<+>>>]>-]>>]<<<<<<<.

```

# Answer
> 6 votes
# C, ~~43~~ 42 bytes

*Saved 1 byte thanks to @Dennis*

Every answer is the same, I must do something different!

Try it online!

```
a(n){return n<3?:a(n-a(n-2))+a(n---a(n));}

```

Explanation: it's basically `a(n-a(n-2))+a(n-a(n-1))` but with swaggyundefinedbehavior (works on my phone (gcc) and ideone).

# Answer
> 5 votes
## Mathematica, 36 bytes

Byte count assumes ISO 8859-1 encoding and Mathematica's `$CharacterEncoding` set to `WindowsANSI` (the default on Windows; other settings might work as well, but some like `UTF-8` definitely don't).

```
±1=±2=1
±n_:=±(n-±(n-1))+±(n-±(n-2))

```

Defines `±` as a unary operator.

I tried getting rid of the duplication, but ended up with the same byte count:

```
±1=±2=1
±n_:=Tr[±(n-±(n-#))&/@{1,2}]

```

# Answer
> 4 votes
# Jelly, ~~15~~ 14 bytes

```
2Rạ⁸ß€$⁺Sµ1>?2

```

Try it online! or verify all test cases (takes a few seconds).

### How it works

```
2Rạ⁸ß€$⁺Sµ1>?2  Main link. Argument: n (integer)

2R              Yield [1, 2].
      $         Combine the previous three links into a monadic chain.
   ⁸                Yield n.
  ạ                 Take the absolute difference of the return value and n.
    ß€              Recursively call the main link on each result.
       ⁺            Duplicate the chain.
                    The first copy maps [1, 2] to [a(n - 1), a(n - 2)].
                    The second copy maps [a(n - 1), a(n - 2)] to
                    [a(n - a(n - 1)), a(n - a(n - 2))].
        S           Take the sum.
         µ          Combine all links to the left into a chain.
            ?       If...
           > 2          n is greater than 2, call the chain.
          1         Else, return 1.

```

# Answer
> 4 votes
# Jelly, ~~14~~ ~~12~~ 11 bytes

```
ịḣ2S;
1Ç⁸¡2ị

```

This is an iterative approach.

Try it online! or verify all test cases.

### How it works

```
1Ç¡2ị   Main link. Argument: n

1       Set the return value to 1.
 Ç¡     Call the helper link n times, updating the return value after each call.
   2ị   Extract the second element of the resulting array.

ịḣ2S;   Helper link. Argument: A (array)

ị       At-index; retrieve the elements of A at the values of A.
 ḣ2     Head 2; extract the first two results.
    S   Take the sum of the result.
     ;  Prepend the sum to A.

```

# Answer
> 4 votes
## JavaScript (ES6), ~~45 Bytes~~ 34 Bytes

A recursive solution in ES6. Any golfing tips much appreciated.

```
a=n=>n>2?a(n-a(n-1))+a(n-a(n-2)):1

```

Thank you to /u/ismillo for shortening even further.

# Answer
> 3 votes
# Python, ~~45~~ 40 bytes

```
a=lambda n:n<3or a(n-a(n-1))+a(n-a(n-2))

```

Simple naïve interpretation of the challenge.

*Saved 5 bytes thanks to @LeakyNun!*

# Answer
> 3 votes
# Haskell, ~~39~~ 37 Bytes

```
h n|n<3=1|n>2=h(n-h(n-1))+h(n-h(n-2))

```

exactly like described in the challenge, using guards

# Answer
> 3 votes
## C#, 51 44 bytes

```
int a(int n)=>n<3?1:a(n-a(n-1))+a(n-a(n-2));

```

i wonder if this can be shortened by making it anonymous thanks pinkfloydx33!

# Answer
> 3 votes
# R, 50 bytes

```
a=function(n)ifelse(n<3,1,a(n-a(n-1))+a(n-a(n-2)))

```

Usage:

```
> a(1)
  1
> a(20)
  12

```

# Answer
> 3 votes
## CJam, ~~19~~ 18 bytes

```
XXri{_($2$$+}*]-3=

```

Try it online!

Uses the iterative approach.

# Answer
> 2 votes
# Ruby, 36 bytes

A direct implementation. Any golfing suggestions are welcome.

```
a=->n{n<3?1:a[n-a[n-1]]+a[n-a[n-2]]}

```

# Answer
> 2 votes
# Golfscript, 29 bytes

```
~[1 1]{..0=(=\..1=(=@+\+}@*2=

```

Try it online!

# Answer
> 2 votes
## Java 7, ~~68~~ ~~61~~ 51 bytes

17 saved thanks to Leaky Nun.

```
int a(int n){return n<3?1:a(n-a(n-1))+a(n-a(n-2));}

```

# Answer
> 2 votes
## 05AB1E, 29 bytes

An iterative solution.

```
XˆXˆÍL>v¯¤ys-è¯y¯yÍè-è+ˆ}¯¹<è

```

Try it online

# Answer
> 2 votes
# APL, 20 bytes

```
{⍵≤2:1⋄+/∇¨⍵-∇¨⍵-⍳2}

```

Explanation:

```
{⍵≤2:1⋄+/∇¨⍵-∇¨⍵-⍳2}
 ⍵≤2:1               If argument is 2 or less, return 1
      ⋄              Otherwise:
               ⍵-⍳2  Subtract [1, 2] from the argument
             ∇¨      Recursive call on both
           ⍵-        Subtract both results from the argument
         ∇¨          Recursive call on both again
       +/            Sum

```

# Answer
> 2 votes
# VBA Excel 87 bytes

Non-recursive, since I want this to work for n=100000, say:

```
Function A(N):ReDim B(N):For i=3 To N:B(i)=B(i-B(i-1)-1)+B(i-B(i-2)-1)+1:Next:A=B(N)+1

```

... and press `return` (byte #87) at the end of the line to get the `End Function` statement for "free". Note that B values are offset by -1 to avoid initializing for n=1 and 2.

Invoke in spreadsheet as normal, eg `=A(100000)` to get `48157`

The recursive version, **61 bytes**,

```
Function Y(N):If N<3 Then Y=1 Else Y=Y(N-Y(N-1))+Y(N-Y(N-2))

```

starts to get unreasonably slow for n\>30, and couldn't be said to work at all for n\>40.

# Answer
> 2 votes
# Oasis, ~~9~~ ~~7~~ 5 bytes

~~**Non-competing**, since the language postdates the challenge.~~ Thanks to **Kenny Lau** for saving 4 bytes. Code:

```
ece+V

```

Expanded form (`V` is short for `11`):

```
a(n) = ece+
a(0) = 1
a(1) = 1

```

Code:

```
e        # Stack is empty, so a(n - 1) is used, and it calculates a(n - a(n - 1))
 c       # Calculate a(n - 2)
  e      # Calculate a(n - a(n - 2))
   +     # Add up

```

Try it online!. Calculates n = 1000 in 0.1 seconds.

# Answer
> 1 votes
## PowerShell v2+, ~~85~~ ~~79~~ 69 bytes

```
param($n)$b=1,1;2..$n|%{$b+=$b[$_-$b[$_-1]]+$b[$_-$b[$_-2]]};$b[$n-1]

```

Takes input `$n`, sets `$b` to be an array of `@(1, 1)`, then enters a loop from `2 .. $n`. Each iteration we tack onto `$b` the latest calculation in the sequence with a simple `+=` and the definition of the sequence. We then output the appropriate number from `$b` (with a `-1` because arrays in PowerShell are zero-indexed). This works if `$n` is `1` or `2` because both of those values are pre-populated into the lower indices of `$b` from the start, so even if the loop tacks on junk, it's ignored anyway.

---

### Recursive solution ~~78~~ 76 bytes

```
$a={param($k)if($k-lt3){1}else{(&$a($k-(&$a($k-1))))+(&$a($k-(&$a($k-2))))}}

```

First time I've used the equivalent of a lambda as the answer, as usually an iterative solution is shorter (as you can see from all the nested parens). ~~But, in this case, the nested parens are almost duplicated in the iterative solution with the nested array calls, so the recursive solution is shorter.~~ Nope, the iterative solution is indeed shorter (see above).

Call it via the execution-operator, like `&$a 20`. Just a straight-up recursive call.

# Answer
> 1 votes
# Maple, ~~43~~ 41 bytes

```
a:=n->`if`(n>2,a(n-a(n-1))+a(n-a(n-2)),1)

```

Usage:

```
> a(1);
  1
> a(20);
  12

```

This problem is certainly a good candidate for memoization. Using option cache, the run times are cut down significantly:

```
aC := proc(n)
      option cache;
      ifelse( n > 2, aC( n - aC(n-1) ) + aC( n - aC(n-2) ), 1 );
end proc:

```

This can be seen using:

```
CodeTools:-Usage( aC(50) );

```

# Answer
> 1 votes
## JavaScript (ES6), 66 bytes

```
n=>[...Array(n+1)].reduce((p,_,i,a)=>a[i]=i<3||a[i-p]+a[i-a[i-2]])

```

Non-recursive version for speed; recursive version is probably shorter but I'll leave it for someone else to write. I always like it when I get to use `reduce`. Note: 1 byte saved by returning `true` (which casts to `1` when used in an integer context) for of `a(1)` and `a(2)`.

# Answer
> 1 votes
# Pyth, 16 bytes

```
L|<b3smy-bytdtBb

L                  def y(b):
 |<b3                b < 3 or …
      m      tBb       map for d in [b - 1, b]:
       y-bytd            y(b - y(d - 1))
     s                 sum

```

Defines a function `y`.

Try it online (added `yMS20` to print the first 20 values)

# Answer
> 1 votes
# Forth, 76 bytes

I finally got it working!

```
: Q recursive dup dup 3 < if - 1+ else 2dup 2 - Q - Q -rot 1- Q - Q + then ;

```

**Try it online**

**Explanation:**

```
: Q recursive                           \ Define a recursive function Q
    dup dup 3 <                         \ I moved a dup here to golf 2 bytes
    if                                  \ If n < 3, return 1
        - 1                             \ Golf: n-n is zero, add one. Same as 2drop 1+
    else
        2dup 2 - Q - Q                  \ Copy n until 4 on stack, find Q(n-Q(n-2))
        -rot                            \ Move the result below 2 copies of n
        1- Q - Q +                      \ Find Q(n-Q(n-2)), then add to previous ^
    then ;

```

**Try it online** (slightly un-golfed from above)

Unfortunately, mutual recursion is a bit too wordy to use for golfing.

# Answer
> 1 votes
## Lua, 59 bytes

```
function a(n)return n<3 and 1 or a(n-a(n-1))+a(n-a(n-2))end

```

# Answer
> 1 votes
# Husk, 17 bytes

```
!¡λṁ!¹m≠→L¹↑_2)ḋ3

```

Try it online!

Feels too long. I'll post something with `fix` soon.

# Answer
> 1 votes
# Japt, 20 bytes

No big shenanigans, just recursively calculates the result.

```
§2?1:ßU-ßUÉ)+ßU-ßU-2
§2                   // If the input is less than or equal to 2
  ?1                 // return 1,
    :                // otherwise
     ß               // recursively recall with
      U-ßUÉ          // input minus recursive recall input minus one
           )+        // plus
             ßU-ßU-2 // same with input minus two.

```

Try it here.

# Answer
> 0 votes
# J, ~~29~~ 28 bytes

```
1:`(+&$:/@:-$:@-&1 2)@.(2&<)

```

Uses the recursive definition.

### Usage

Extra commands are used for formatting multiple input/output.

```
   f =: 1:`(+&$:/@:-$:@-&1 2)@.(2&<)
   (,:f"0) >: i. 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 1 2 3 3 4 5 5 6  6  6  8  8  8 10  9 10 11 11 12

```

### Explanation

```
1:`(+&$:/@:-$:@-&1 2)@.(2&<)  Input: n
                        2&<   If n < 2
1:                              Return 1
                              Else
               -&1 2            Subtract [1, 2] from n to get [n-1, n-2]
            $:@                 Call recursively on n-1 and n-2
           -                    Subtract each of the results from n
        /@:                     Reduce using
      $:                          A recursive call on each
    +&                            Then summation
                                Return that value as the result

```

# Answer
> 0 votes
# dc, 62 bytes

```
?si2sa1dd2:a:a[la1+dsadd1-;a-;alad2-;a-;a+r:ali;a0=A]dsAxli;af

```

This solution makes use of arrays and recursion.

```
?si          # Take input from stdin and store it in register `i'
2sa          # Initialise register `a' with 2, since we'll be putting in the first
             #   two values in the sequence
1dd2         # Stack contents, top-down: 2 1 1 1
:a           # Pop index, then pop value: Store 1 in a[2]
:a           # Ditto:                     Store 1 in a[1]
[            # Open macro definition
 la 1+ dsa   # Simple counter mechanism: Increment a and keep a copy on stack

# The STACK-TRACKER(tm): Top of stack will be at top of each column, under the
#   dashed line. Read commands from left to right, wrapping around to next line.
#   This will be iteration number n.
  dd   1-    ;a       -          ;a            la            d
#-----------------------------------------------------------------------
# n    n-1   a[n-1]   n-a[n-1]   a[n-a[n-1]]   n             n
# n    n     n        n          n             a[n-a[n-1]]   n
# n    n     n                                 n             a[n-a[n-1]]
#                                                            n
#

  2-            ;a            -             ;a            +      r    :a
#-----------------------------------------------------------------------
# n-2           a[n-2]        n-a[n-2]      a[n-a[n-2]]   a[n]   n
# n             n             a[n-a[n-1]]   a[n-a[n-1]]   n      a[n]
# a[n-a[n-1]]   a[n-a[n-1]]   n             n
# n             n

 li;a        # Load index of target element, and fetch that element's current value
             #    Uninitialised values are zero
 0=A         # If a[i]==0, execute A to compute next term
]dsAx        # Close macro definition, store on `A' and execute
li;a         # When we've got enough terms, load target index and push value
f            # Dump stack (a[i]) to stdout

```

# Answer
> 0 votes
# Erlang, 46 bytes

```
f(N)when N<3->1;f(N)->f(N-f(N-1))+f(N-f(N-2)).

```

# Answer
> 0 votes
# Racket, 63 bytes

```
(define(a n)(if(> n 2)(for/sum([m'(1 2)])(a(- n(a(- n m)))))1))

```

# Answer
> 0 votes
# \>\<\>, 65+2 = 67 bytes

```
^n;
.+]{0$
v1}\
v2} @2->1[
v3}>-  /:::1-1[
>4}:2)?^~~1]{0$.
/0$1[

```

Input neds to be present on the stack at program start, so +2 bytes for the `-v` flag. Try it online!

More ridiculously slow recursive madness. Test case for 20 on TIO takes 20.5 seconds, so use larger inputs at your own risk

# Answer
> 0 votes
# Clojure, 86 bytes

```
(defn a[n](cond(< 0 n 3)1 1(+(a(- n(a(dec n))))(a(- n(a(- n 2)))))))

```

Very literal.

```
(defn a [n]
  (cond
    (< 0 n 3) 1 ; Return 1 if n is 1 or 2
    :else (+ (a (- n (a (dec n)))) ; Else, recurse 4 times and do some math
             (a (- n (a (- n 2)))))))

(doseq [n (range 1 21)]
  (println n (a n)))

1 1
2 1
3 2
4 3
5 3
6 4
7 5
8 5
9 6
10 6
11 6
12 8
13 8
14 8
15 10
16 9
17 10
18 11
19 11
20 12

```

# Answer
> 0 votes
## Lithp, 70 bytes

```
(def a #N::((if(<= N 2)(1)((+(a(- N(a(- N 1))))(a(- N(a(- N 2)))))))))

```

Warning: incredibly slow. Very recursive. Implements the exact algorithm in challenge.

Try it online!

An alternate solution that is much faster, using caching of results:

## Lithp, 166 bytes

```
((def a #N::((if(<= N 2)(1)((+(b(- N(b(- N 1))))(b(- N(b(- N 2)))))))))(var C(dict))
 (def b(scope #N::((if(!(dict-present C N))((dict-set C N(a N))))(dict-get C N)))))

```

Try it online!

# Answer
> 0 votes
# PHP, 56 bytes

```
function q($n){return$n<3?:q($n-q($n-1))+q($n-q($n-2));}

```

recursive function; requires PHP 5.6 or later (or replace `?:` with `?1:`).

# Answer
> 0 votes
# Japt `-N`, 14 bytes

Note that the `-N` flag isn't *strictly* necessary here as it would seem returning `true` instead of `1` is allowed.

```
§2ªCìx@ßU-ßXnU

```

Try it or run all test cases

```
§2ªCìx@ßU-ßXnU     :Implicit input of integer U
§2                 :Less than or equal to 2?
  ª                :Logical OR with
   C               :12
    ì              :To digit array
     x             :Reduce by addition
      @            :After passing each X through the following function
       ß           :  Recursive call with argument
        U-         :    U minus
          ß        :      Recursive call with argument
           XnU     :        U minus X

```

---
Tags: code-golf, sequence, arithmetic
---


# Question
Title: Smallest python script to print even numbers 0 to 100
I'm work on a problem which I set myself for fun, which is to create a python script which prints the even numbers from 0 to 100. The challenge is to make the script as small as possible. This is what I have so far:

```
for x in range(0, 101):
    if (x % 2 == 0):
        print x

```

Currently it is 60 bytes. Can anyone think of a way to make it smaller?

---

Edit: `print(*range(2,101,2),sep='\n')` which is 30 bytes. Any smaller?

# Answer
# Python 2 - 12 characters

```
print 8**999

```

The decimal representation of all even numbers from 0 to 100 can be found in the output:

```
153778990270139647116444851659594064330089236967104214470764753645007350076834118596920008479824182447803706156756475613564110522612279602948135310258168541404369918794480627176627915013920083365328091029969610052054309789461709376676636344651086297099162082351332867728061686056465813162964114500668343488577962834185114919242101638217077550294093097112980059735456387540301162747936045475366317560310988720435512281742591085641505551107966844283901574058972330493685836063965131445246304097593431852972101058022587137885482726523043570690342524474585327775688980689010069001288756281975198668705741000141718184277589376710426738442847382969979234512669279398030637083755270090078676447687796406001053805898105262326290072552249025832780916090265261064205460488458795026145331708830141367124625271312584437671840499845750728447412590406684361326531266896486146862384988911439049971734022314877278748672

```

As a bonus, so can the odd numbers.
> 40 votes

# Answer
# Python 3, 22 (Possibly not allowed)

If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even numbers from 0 to 100, *newline separated*", then the shortest solution is:

```
print(*range(0,101,2))

```

Remember, it's very important in code golf not to put any limitations on yourself you don't have to - do what the problem asks, and no more.
> 31 votes

# Answer
# Python2 26

```
i=0;exec"print i;i+=2;"*51

```

independent discovery of @bitpwner's solution
> 23 votes

# Answer
# Python 2 - 26

```
i=0;exec"print i;i+=2;"*51

```

Based on the tip on exec with string multiplication found at Tips for golfing in Python.
> 20 votes

# Answer
# Python 2, 26 (possibly not allowed)

```
i=102
while i:i-=2;print i

```

It wasn’t strictly specified, in which order the numbers were to be printed.
> 13 votes

# Answer
# Python 2 - 20 (Possibly not allowed)

This is python 2 specific and probably cheating since it prints the list, but since all numbers end up on the screen:

```
print range(0,101,2)

```
> 10 votes

# Answer
# Python 2, 28

```
for i in range(51):print 2*i

```
> 9 votes

# Answer
# Python 2 in \*NIX, 24

```
os.system('seq 0 2 100')

```

If you need to add

```
import os

```

Then the total is 33 characters.
> 7 votes

# Answer
# Python 3, 29

```
*a,=map(print,range(0,101,2))

```

If you're in Python 2 and happen to have already imported the print function, you don't have to make the iterator object into a list and it becomes 25 characters:

```
map(print,range(0,101,2))

```

I don't know that that's entirely fair though.

Here's another fun idea that works in python 2 or 3. It's a tad longer.

```
def p(i):
    if i+2:p(i-2);print i
p(100)

```
> 5 votes

# Answer
## Python 2 - 24

```
0;exec"_+=2;print _;"*50

```

(based on bitpwner and Sparr solution)

In the shell, "\_" contains the value of the previously evaluated expression
> 4 votes

# Answer
# Python 2 - 20 (questionable)

If isaacg's space-separated solution is OK, then presumably the normal list formatting is as well:

```
print range(0,101,2)

```

For further rule-twisting, apply SirBraneDamuj's suggestion from the comments:

```
print range(101)

```

and you are at 16. So what if there's some extra garbage separating them?
> 2 votes

# Answer
# Python List Comprehension - 39

This uses a list comprehension, one trick to make it shorter is to multiply the index by 2 rather than going from 0 to 100 and a trailing `if x % 2` check for even.

```
print'\n'.join(`2*x`for x in range(51))

```

Using a map and @isaacg's suggestion it ends up being the same 39 characters:

```
print'\n'.join(map(str,range(0,101,2)))

```

Edit: After separating values by newline it is by far not the shortest.
> 1 votes

# Answer
In my case, I use list comprehension (in Python 3.9). \< Sorry, I came late but I hope it makes sense.\>

```
even_numbers = [x for x in range(100) if x % 2 == 0]
print(even_numbers)

```
> 0 votes

---
Tags: code-golf, tips, python
---