Replies: 3 comments 1 reply
-
Just use closed-form Binet's formula if you need it really fast ;) In programming performance is usually achieved by changing the algorithm and not by doing micro-optimizations. |
Beta Was this translation helpful? Give feedback.
-
Very cool, @haijinSk ! I've never met an "almost non-programmer" who can write DCGs before!! I barely know any PROGRAMMERS that can write DCGs! This is great! I was inspired to play around with it! Not nearly as elegant as your formulation but it does address the list carry and the redundant choice point from the defaulty representation. Thanks for the write-up! :- use_module(library(dcgs)).
:- use_module(library(time)).
:- use_module(library(lambda)).
:- use_module(library(reif)).
:- use_module(library(lists)).
:- use_module(library(clpz)).
clpz:monotic.
% using "successor notation" so that you don't have a "defaulty respresentation"
% in the DCG
succs(N, S) :- succs_(0, N, S).
succs_(A,B,s(C)) :-
#A1 #= #A+1,
if_(A=B,
C=0,
succs_(A1,B,C)
).
?- succs(3,S).
S = s(s(s(s(0)))).
%% using semicontext notation "stack" so you don't need to carry the whole list in memory
fib_(A,B), [B,C] --> { #C #= #A + #B }.
fib(0) --> [_].
fib(s(N)) --> [A,B], fib_(A,B), fib(N).
?- phrase(fib(s(s(0))), [0,1], [Res]).
Res = 2.
%% convenient API
fib(N, Res) :-
0 #=< N, % so that you don't have to specify domain of N at toplevel
succs(N,S), % so that you don't have "defaulty" representation
phrase(fib(S), [0,1], [Res]). % so that you don't carry the whole list
?- fib(N, R).
N = 0, R = 1
; N = 1, R = 2
; N = 2, R = 3
; N = 3, R = 5
; N = 4, R = 8
; N = 5, R = 13
; N = 6, R = 21
; N = 7, R = 34
; ... .
% boring co-recursive version!
fib(A,B,N,R) :-
#C #= #A + #B,
#N0 #= #N - 1,
if_(N=0,
R=B,
fib(B,C,N0,R)
).
fib1(N,R) :-
fib(1,1,N,R).
?- fib1(N,R).
N = 0, R = 1
; N = 1, R = 2
; N = 2, R = 3
; N = 3, R = 5
; N = 4, R = 8
; N = 5, R = 13
; ... .
?- fib(N,R),fib1(N,R).
N = 0, R = 1
; N = 1, R = 2
; N = 2, R = 3
; N = 3, R = 5
; N = 4, R = 8
; N = 5, R = 13
; N = 6, R = 21
; ... .
?- \((N=10001, fib(N,R), fib1(N,R))).
true.
?- \time(fib(10001, _)).
% CPU time: 0.042s, 200_076 inferences
true.
?- \time(fib1(10001, _)).
% CPU time: 0.042s, 150_062 inferences
true. |
Beta Was this translation helpful? Give feedback.
-
This is awesome to read, the latest state-of-the-art constructs such as lambda, quads, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
"[Larry] Wall argues that Perl itself has no “theoretical axes to grind” or “any agenda at all, other than to be maximally useful to the maximal number of people. To be the duct tape of the Internet, and of everything else.” Wall asks, “How is duct tape like the Force? It has a light side, and a dark side, and it holds the universe together.”"
Norberto Gomez Jr.: The Art of Perl: How a Scripting Language (inter)Activated the World Wide Web, 2013 (dissertation)
Not a math/number person, but I was dreaming about "quick fibs": #2896
and at the end of the day (or in the morning, so to speak) I ended up in a pessimism--hope "quantum state": #2947
I'm almost a non-programmer yet fascinated by and with programming languages, as they are digital incarnations of ideas ready to be used for "real things", a kind of magic...
It's not relevant here, but I have no sympathy for any "AI" things (as I see it, at best, the "AI" is a gargantuan, brutally-commercial recyclation of human created content without any, any compensation for the authors). As I see it, the best AI are programming languages themselves. "The world needs less AI and better programming languages": https://mitpress.mit.edu/9780262548717/moral-codes/
Back to the Fibs. I saw this Raku "fibs-idiom" recently:
0,1,*+* ... *
It's a sequence:
and it's lazy:
For example, the first 20 elements:
And, in no time, at the position 10_000 (starting from zero):
Lazy lists or not, now, I want that declarative 10000 "in no time" in Scryer and Trealla.
My file to consult (I let it be short, except when libraries are explicit):
And the "one-liner". For "pretty-printing" I'm using notation from lambda for selecting only one varable to be displayed:
It's not "in no time" in both directions, though. Of course, it can be written without using CLPZ and DCGs, without constructing a list and some steroids in a form of the cut
!
might be added, for extended "in no time" behavior, but I'm not the right person for the cut things.Thank you.
Beta Was this translation helpful? Give feedback.
All reactions