,[->+>+<<]>>[-<<+>>]++++++++[<------<------>>-]<[->>+>>+<<<<]>>[-<<+>>]>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>[-<<+>>]>>>]<<<++++++++[-<<++++++>>]<<.
Try it online!
No built-ins ;)
How it works
, - get ascii input
[->+>+<<] - duplicate input
>>[-<<+>>] - shift inputs left to start
++++++++[<------<------>>-] - convert ascii into input numbers
<[->>+>>+<<<<] - get loop intervals (same as input #)
>>[-<<+>>] - shift input back again
>>-[-<<<<<[>[>+>+<<-]>>[<<+>>-]<<<-]>> - iterated addition (multiplication)
[-<<+>>]>>> - Shift output back into input
]<<<++++++++[-<<++++++>>]<<. - convert final output to ascii
In a nutshell, this works by multiplying x
(the input) by itself x
times. (a.k.a. iterating iterated addition). The net result is x^x.
I/O
The program takes a single ASCII input, and processes it as it's ASCII index minus 48. The minus 48 is to normalize inputs of actual numbers (4
becomes 52
-> 52-48
-> 4
). To input a number higher than 9, use the next corrosponging ASCII character (:
-> 58-48
-> 10
). The program ouputs in a similar fashion.
Test I/O
INPUT > PROCESSED INPUT >> OUTPUT > TRANSLATED OUTPUT
1 > 1 >> 1 > 1
2 > 2 >> 4 > 4
3 > 3 >> K > 27
Since there are no printable ASCII characters after an input of 3, it can only print numbers in theory. Though, you can check all inputs do in fact work on visualizers such as this.
Can we accept input as a string? – Shaggy – 2017-05-09T22:32:29.770
I have made an edit to this, hoping it will be reopened. I deleted rule 3 and instead stated that it should be a full program, as the OP probably intended – Mr. Xcoder – 2017-05-10T10:03:22.453
Much better, @Mr.Xcoder but I suggest removing (or rewording) the second restriction. Does "not a function" exclude JS from participating? I'd also suggest, for the purposes of the challenge, that we should have to handle
0
and that the expected output be specified (0
or1
or either). Finally, having to handle negative integers would be a nice addition to the challenge. – Shaggy – 2017-05-10T10:07:52.620@Shaggy added js back in... calculated 0^0 on the apple calculator and it returned 1. Maybe 1 should be the chosen value, because Python also returns
1
for0^0
. However,Foundation
+ Swift returns 0 – Mr. Xcoder – 2017-05-10T10:08:45.687@Mr.Xcoder, outputting
0
might present more of a challenge in some languages (it'd add 3 bytes to my answer!) but either option works for me. – Shaggy – 2017-05-10T10:12:16.727>
0
". Should that be "need not handle" instead? 2. At present the majority of the answers to this question don't follow the rule that the answer must be a full program. I'm not sure why @Mr.Xcoder added that requirement, but I don't think the question should be reopened until the question is consistent with the existing answers (either because the requirement is removed or because the offending answers have been deleted).@PeterTaylor that's what the OP initially stated, ambiguously though, I will remove that rule. For (1) I will reword that – Mr. Xcoder – 2017-05-10T10:43:43.427
@PeterTaylor Done, now it's consistent with the answers. – Mr. Xcoder – 2017-05-10T10:44:56.813
1@Mr.Xcoder, I've removed the "restriction" that we need not handle
0
and instead specified that0<x
in the lead-in. I also removed the restriction that code shouldn't throw errors; that should go without saying. Feel free to roll back if necessary. – Shaggy – 2017-05-10T11:14:25.450@Shaggy it's a good edit, now covers everything well – Mr. Xcoder – 2017-05-10T11:16:43.757
@Shaggy and what was wrong with my good luck wish?)))) – None – 2017-05-12T23:16:08.550
@Mr. Xcoder for C, i suppose this will be an function int f(x), which will compile in gcc and ms visual studio. so we can put that code in some code.h file. then make our test program like follows:
#include <stdio.h> #include "code.h" int main() { int i; for(i=1; i<11;i++) printf("%u %u\n", i, f(i)); return 0; }
bytesize of code.h is measured – None – 2017-05-12T23:50:23.850Next time: Evaluate the xth hyperoperation of x – Matthew Roh – 2017-05-15T10:00:40.270
@xakepp35 people (for some reason) on this site dislike wishing people luck. And it was Peter Taylor, not Shaggy who deleted it. – caird coinheringaahing – 2017-07-09T20:37:26.150