fastfib(0,1,0).
fastfib(1,0,1).
fastfib(2,1,1).
fastfib(A,B,C):-
A > 2,
(divmod(A,2,X,1),fastfib(X,E,F),FF is F*F,B is 2*E*F+FF,C is E*E+FF+B;
divmod(A,2,X,0),fastfib(X,E,F),FF is F*F,B is E*E+FF, C is 2*E*F+FF).
fib(A,C):- fastfib(A,B,C).
@TheLethalCoder Right now, I am writing the lexer for the new version of Carrot and I am faced with a dilemma: how will I differentiate the operator / from the regex /regex/flags since they both start with the same character (and I have planned so that operators can take more than one argument)? One of my thoughts is to use spacing to differentiate operators from arguments, the other is to use a different delimiter for regex, like @.
@TheLethalCoder For the - operator, I just check if there is a . or a digit following the minus. If there is a . or digit, then it is a Float token, otherwise it is the minus operator. So this means that I can have a space after the - to differentiate it from floats. But I can't do that for / vs regex
x <int> <int> Bitwise XOR. A ^ B in Python.
x <lst> <any> First occurrence. Return the index of the first element of A equal to B, or -1 if none exists.
x <str> <str> First occurrence. Return the index of the first substring of A equal to B, or -1 if none exists.
x <non-lst> <lst> All occurrences. Returns a list of the indexes of elements of B that equal A.
x <str> <non-lst> First occurence. Return the index of the first substring of A equal to str(B), or -1 if none exists.
Another question about Pyth (inspired by the tips CJam question): last index of a number in a number array. Consider the following array: K[2 3 4 2 3 4). I have written t-lKx_K2 to get the last index of 2 in the list. Is there no built-in/shorter way to do this?
Description
There have been quite a few other challenges concerning these numbers before, and I hope this one is not among them.
The n th triangular number equals the sum of all natural numbers up to n, simple stuff. There are a wikipedia page and an entry at OEIS, for those who wish to inform ...
@TheLethalCoder I only saw this message now. Thinking about this now, I am thinking about a command that will "explode" the array on the sheet horizontally (or vertically). Then the program can later move across the sheet to change the individual elements of the arrays directly
@Cowsquack I'm not sure, different delimiters solves the problem for now, but if you later want to use that character for a different command you get stuck again. I would wrap it in quotes and use the string as an regex argument like: /"pattern"flags seeing as you aren't using quotes for anything else at the moment. with the / command.
@Cowsquack I'm not entirely happy with that idea but that's what seems best to me so far...
@Cowsquack Okay so more of a way to move across the stack instead of accessing parts of it separately?
@TheLethalCoder well I was thinking about putting the tape to use, given the fact that Carrot can only operate on a single stack at any moment, so I came up with the exploding array idea
And I also don't understand what C is supposed to be in this:
> Cumulative reduce. Reduce B from left to right, with function A(_, _) and C as starting value, and return all intermediate values, starting with C. A takes current value, next element of B as inputs.
.u <l:NY> <seq/num> <any> Cumulative reduce. Reduce B from left to right, with function A(_, _) and C as starting value, and return all intermediate values, starting with C. A takes current value, next element of B as inputs.
.u <l:NY> <any> <none> Cumulative fixed-point. Apply A(_, _) until a result that has occurred before is found. Starting value B. Return all intermediate results, starting with B. A takes current value, iteration number as inputs.
@TheLethalCoder Extending from your idea, regex arguments could use ' as the delimiter. This will Carrot to be able to distinguish string from regex arguments.
@TheLethalCoder well, once all of Carrot's operators are implemented, every operator will be able to take in every type and combination and number of arguments...
> Cumulative fixed-point. Apply A(_, _) until a result that has occurred before is found. Starting value B. Return all intermediate results, starting with B. A takes current value, iteration number as inputs.
Stare at this ^ (message for me)
Why does that lambda have two arguments @LeakyNun ?
@Cowsquack In fact I can't think of another way of using a regex at the moment so just '/pattern/flags' could be fine on its own. Unless you can think of another way to use it?
Types:
num = int, float
cmp = complex
seq = str, list, tuple
lst = list, tuple
idx = str, list, dict
col = str, list, set, dict
any = any expression (a for short)
blk = a code block (any series of statements)
... = any number of arguments of any type.
n:_ = n _ arguments.
var = variable. Not evaluated.
func = function. Not evaluated.
func n = function with arity n.
pfn = Preceding function of arity n. *F, for instance.
l:_ = Lambda with lambda variable(s) _
pos = positive int.
nneg = nonnegative int.
@Cowsquack Me neither, was just a thought though. It depends if you end up wanting to do something like +'/pattern/flags' for finding all matches, -'/pattern/flags'"replacement" for replacing matches and some other ideas...
> Cumulative fixed-point. Apply A(_, _) until a result that has occurred before is found. Starting value B. Return all intermediate results, starting with B. A takes current value, iteration number as inputs.
@TheLethalCoder well, replacement was one of the reasons I decided that commands should have multiple arguments, so as you stated, a regex containing a pattern and a string containing the replacement.
The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz. It can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
The conjecture is also known as the 3n + 1 conjecture, the Ulam conjecture (after Stanisław Ulam), Kakutani's problem (after Shizuo Kakutani), the Thwaites conjecture (after Sir Bryan Thwaites), Hasse's algorithm (after Helmut...
> The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz. It can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
> Cumulative fixed-point. Apply A(_, _) until a result that has occurred before is found. Starting value B. Return all intermediate results, starting with B. A takes current value, iteration number as inputs.
> Asked whether the laws of mathematics behind encryption would trump any new legislation, Mr Turnbull said: "The laws of Australia prevail in Australia, I can assure you of that.
> "The laws of mathematics are very commendable but the only law that applies in Australia is the law of Australia."
@Poke Coincidentally, there's math fiction related to the idea of overriding the laws of mathematics, and it's written by Greg Egan, who is also from Australia...
The sandbox has way too many non-deleted posts. I'd like everyone to look at your sandbox posts and delete / post / edit them to help clean up the sandbox.
@programmer5000 I have lots, all of which I am likely to work on in future, although they won't be edited until I get round to them (I'm working through roughly in votes order). So old inactive posts aren't necessarily ready to be deleted, posted, or edited. Maybe we could just pick one at random and bring it to the attention of TNB, and gradually get them all discussed over time?
Help! I just logged into Stack Exchange, but I forgot what my password is! I need a way to work it out before I log off.
Luckily, I'm an excellent hacker. Not only was I able to find my password's hash, but I also found Stack Exchange's hashing algorithm! It takes the ASCII value of each digit ...
We define a prime character as a character that has a prime ASCII value. We define a prime string as a String that only contains prime characters.
Given a String that only contains printable ASCII, determine whether that String is prime.
Input / Output
You can take input either as a String or...
Let's see how good your language of choice is at selective randomness.
Given 4 characters, A, B, C, and D, or a string of 4 characters ABCD as input, output one of the characters with the following probabilities:
A should have a 1/8 (12.5%) chance to be chosen
B should have a 3/8 (37.5%) chanc...
Thats because people chose to give up rep as a bounty
Case and point, this answer is Ok, put it certainly doesn't deserve 580 rep. Most of the votes are there because of the HNQ, not because the answer is excellent. I have only received a fraction of the rep that I would have because of the rep cap, much closer to what I deserve from the post.
@TheLethalCoder Status update: the lexer is now, more or less, finished. I've added arrays {123 "string" '/pattern/flags'} in addition to regex arguments.
Challenge
Given an integer in 32-bit two's complement format, return the index of the second least-significant zero digit in the binary representation, where an index of 0 represents the least significant bit, and an index of 31 represents the most significant bit.
If there is no second zero, y...