• What Are You Working On? April 2015
    1,741 replies, posted
all of these recursive fibonacci functions are ridiculously inefficient please memoize them
[QUOTE=killerteacup;47494538]all of these recursive fibonacci functions are ridiculously inefficient please memoize them[/QUOTE] How about a logarithmic one [code]module Fibonacci where type Mat = (Integer, Integer, Integer, Integer) type Vec = (Integer, Integer) fibMat :: Mat fibMat = (0, 1, 1, 1) matMult :: Mat -> Mat -> Mat matMult (a, b, c, d) (e, f, g, h) = (a * e + b * g, a * f + b * h, c * e + d * g, c * f + d * h) matSqr :: Mat -> Mat matSqr m = matMult m m matExp :: Mat -> Integer -> Mat matExp m 1 = m matExp m n = if n `rem` 2 == 0 then matSqr $ matExp m (n `div` 2) else matMult m $ matSqr $ matExp m ((n - 1) `div` 2) transform :: Mat -> Vec -> Vec transform (a, b, c, d) (x, y) = (a * x + b * y, c * x + d * y) fib :: Integer -> Integer fib 0 = 0 fib n = if n < 0 then ((-n `rem` 2) * 2 - 1) * fib (-n) else fst $ transform (matExp fibMat n) (0, 1)[/code]
In Clojure: (recursive and tail recursive) [code] (defn fib [n] (cond (= 0 n) 0 (= 1 n) 1 :else (+ (fib (- n 1)) (fib (- n 2))))) (defn fib-tr [n] (loop [a 0 b 1 i 0] (if (= n i) a (recur b (+ a b) (inc i))))) [/code]
What are some stylistic choices I can make to make my C code more navigateable? I've got a bunch of methods that I think are formatted really well, but no matter what I do, my methods, their declarations, and the main method all run together when I'm scrolling through. I've only got about a 175 lines of code thus far so I don't really need to compile in multiple files I don't think... What do you think I should do?
[QUOTE=killerteacup;47494538]all of these recursive fibonacci functions are ridiculously inefficient please memoize them[/QUOTE] Fiiiine, here's a tail-recursive version in Erlang: [code] fib(N) -> fib(N, 0, 1). fib(0, Result, _Next) -> Result; fib(N, Result, Next) when N > 0 -> fib(N-1, Next, Result+Next). [/code]
First time I see people dick waving with their recursive Fibonacci functions.
I made a generic multi-matching ([editline]edit[/editline] now recursive) descend parser generator framework in C# over the last few hours (and it's still really unpolished and incomplete so you need more type parameters than you should, have to call [I]GetEnumerator()[/I] manually and the co- and contravariant interfaces seem to behave strangely (which may or may not be my fault)). In heavy imitation of Sprache's parser definitions: [code]using ScanT; using System; using System.Linq; namespace ScanT_Test { static class Program { static void Main(string[] args) { var input = "ab"; var a = Template.One<char>(c => c == 'a'); var b = Template.One<char>(c => c == 'b'); var ab = from ca in a from cb in b select Tuple.Create(ca, cb); var ba = from cb in b from ca in a select Tuple.Create(cb, ca); Console.WriteLine("ab on \"ab\":"); foreach (var match in ab.MatchOn(input.GetEnumerator())) { Console.WriteLine(match); } Console.WriteLine("ba on \"ab\":"); foreach (var match in ba.MatchOn(input.GetEnumerator())) { Console.WriteLine(match); } Console.WriteLine(); var any = Template.One<char>(c => true); var anyOrA = Template.Or<char, int, char>(new[] { any, a }); var maybeAnyOrA = Template.Maybe(anyOrA); var complex = from first in maybeAnyOrA from second in maybeAnyOrA from third in maybeAnyOrA from fourth in maybeAnyOrA from nothing in Template.End<IScanner<char, int>>() select Tuple.Create(first, second, third, fourth); var scannable = new ScannableString(input); Console.WriteLine("complex on \"ab\": "); foreach (var match in complex.MatchOn(scannable.GetScanner())) { Console.WriteLine(match); } } } // The following classes are necessary for complex since Template.Or(...) and Template.Maybe(...) require seeking. Template.End() works with any IEnumerable in place of the IScanner<..., ...> though. class ScannableString : IScannable<char, int> { string _value; public ScannableString(string value) { _value = value; } public IScanner<char, int> GetScanner() { return new StringScanner(_value); } public System.Collections.Generic.IEnumerator<char> GetEnumerator() { return GetScanner(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetScanner(); } } class StringScanner : IScanner<char, int> { string _value; int _position = -1; public StringScanner(string value) { _value = value; } public int Position { get { return _position; } set { if (value < -1 || value >= _value.Length) { throw new InvalidOperationException(); } _position = value; } } public object Current { get { return _value[_position]; } } public bool MoveNext() { if (_position + 1 < _value.Length) { _position++; return true; } else { return false; } } public void Reset() { _position = -1; } char System.Collections.Generic.IEnumerator<char>.Current { get { return _value[_position]; } } public void Dispose() { } } }[/code] [code]ab on "ab": (a, b) ba on "ab": complex on "ab": (, , (a), (b)) (, , (a), (b)) (, (a), , (b)) (, (a), (b), ) (, (a), , (b)) (, (a), (b), ) ((a), , , (b)) ((a), , (b), ) ((a), (b), , ) ((a), , , (b)) ((a), , (b), ) ((a), (b), , )[/code] As you can see it gives you all possible matches instead of just the first one, but since it's lazy you can just stop somewhere if you want.
[QUOTE=Contron;47491933]Java EE jobs are secretly disguised as work for masochists, because it's painful to get even the most basic project up and running.[/QUOTE] I love Java and use it daily, but I agree. Due to it being used widely in an enterprise setting, it is filled with specifications and complex procedures which simply feel overwhelming when you are starting. Those libraries are actually quite robust and do a lot of things, that's why they are so complex to use. Typically, unless you are doing a project that requires that type of thing, you choose other simpler libraries that are much much easier to use.
[QUOTE=bunguer;47494999]I love Java and use it daily, but I agree. Due to it being used widely in an enterprise setting, it is filled with specifications and complex procedures which simply feel overwhelming when you are starting. Those libraries are actually quite robust and do a lot of things, that's why they are so complex to use. Typically, unless you are doing a project that requires that type of thing, you choose other simpler libraries that are much much easier to use.[/QUOTE] Personally I find it really hard to find information. I am startung with jboss Java EE6 and when I was looking at the official tutorials I saw this: Helloworld Kitchensink with beans CDI 2.0 example Picketlink JSF ... etc Strange abbreviatures, weird tutorials. Why teach technologies when you should teach how to do stuff and introduce technologies as tools for it? I simply want to make a web page that prints a table from db + a form to add an item to the table. Previous RoR experience made me think there would be "Basic Web App In 5 Easy Steps!" tutorial, but boy was I wrong.
ruby [code]require "net/http" def fib(n) src = Net::HTTP.get URI "http://www.miniwebtool.com/list-of-fibonacci-numbers/?number=#{[[n, 1].max, 201].min}" values = (src.scan /<td>F<sub>\d+<\/sub><\/td><td>(\d+)<\/td>/).map { |a| a[0].to_i } end puts "#{fib(5)}" puts "#{fib(10)}" puts "#{fib(15)}"[/code] [t]http://i.imgur.com/L3zzXBJ.png[/t] works on any value of n from 1 to 201
[QUOTE=MuffinZerg;47495057]Personally I find it really hard to find information. I am startung with jboss Java EE6 and when I was looking at the official tutorials I saw this: Helloworld Kitchensink with beans CDI 2.0 example Picketlink JSF ... etc Strange abbreviatures, weird tutorials. Why teach technologies when you should teach how to do stuff and introduce technologies as tools for it? I simply want to make a web page that prints a table from db + a form to add an item to the table. Previous RoR experience made me think there would be "Basic Web App In 5 Easy Steps!" tutorial, but boy was I wrong.[/QUOTE] This seems like a good resource to learn the various technologies and put them into practice as a real-world web app [url]http://www.jboss.org/ticket-monster/[/url] However, if you only want to do stuff like printing something from a db and stuff like that, you are probably better off with creating a REST endpoint with Jersey+Tomcat [url]http://www.vogella.com/tutorials/REST/article.html[/url].
C#: [code] static long[] fibTable = new long[] { // optimizations, woo! 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181 }; static long Fib(int n) { if (n < 0) throw new Exception("you're a fuckin' moron m8"); if (n < fibTable.Length) return fibTable[n]; return Fib(n - 2) + Fib(n - 1); } [/code] Call me crazy but I've seen game code in production (not written by me) using the same lookup table technique for factorials.
[QUOTE=proboardslol;47494712]What are some stylistic choices I can make to make my C code more navigateable? I've got a bunch of methods that I think are formatted really well, but no matter what I do, my methods, their declarations, and the main method all run together when I'm scrolling through. I've only got about a 175 lines of code thus far so I don't really need to compile in multiple files I don't think... What do you think I should do?[/QUOTE] Whenever I feel like my code is getting hard to read in one file, I add white space between sections, and blocks of comments, kinda like fake file headers. or just give up and break it into multiple files.
Call me stupid but what is Fibbonaci function used for anyway?
[QUOTE=Fourier;47495432]Call me stupid but what is Fibbonaci function used for anyway?[/QUOTE] Asserting dominance on programming forums, of course!
[QUOTE=Fourier;47495432]Call me stupid but what is Fibbonaci function used for anyway?[/QUOTE] Mathematically it shows up sometimes and has a strong relation to the phi constant. In programming it's a good example of recursion that nicely shows the possible pitfalls and their solutions. It's a good introduction to optimization techniques like dynamic programming and memoization. A slightly more advanced subject is expressing arithmetic recursive expressions as matrix multiplication and how that let's use mathematical machinery that can drastically improve performance.
[QUOTE=Tamschi;47493860]Consider depth-ordering by importance. I think the order in Touhou games is - fully transparent or screen-space effects - bombs - enemy projectiles - your projectiles - you - enemies - background effects without gameplay relevance - background The amount of detail in your game is also very high. Consider lowering it for unimportant entities and colour/shape-coding them to some extent.[/QUOTE] Thank you
It's also a good way to compensate for your social inadequacy by pretending you're smarter than everyone else.
You're all idiots. [code] Console.WriteLine("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811"); [/code]
[QUOTE=reevezy67;47495700]You're all idiots. [code] Console.WriteLine("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811"); [/code][/QUOTE] One man's [I][B][U]CRAZY[/U][/B][/I] trick to calculate the Fibonacci sequence without any math! Programmers [I][B][U]HATE[/U][/B][/I] him!
[B]Working on some wacky deathmatch/boss battle game, with stupid weapons, characters and maps. Probably been done before, but I don't give a rats-ass.[/B] [URL]http://a.pomf.se/fhpxbf.mp4[/URL]
Yeah, who cares. Sounds cool.
[QUOTE=Tamschi;47494846]I made a generic multi-matching ([editline]edit[/editline] now recursive) descend parser generator framework in C#[/QUOTE] Ah, one of [i]those[/i] I always feel a bit dumb when I don't even understand what the thing someone is posting about is, let alone the code Looks clever and neat though! I'm going to have to read up on recursive decent parsers now...
[QUOTE=Berkin;47493627]Since you mentioned me (saw it on the ticker), I feel like I should add my two cents to your sentiment. You're right in that doing work like mine is a lonely process. But that's where I thrive. I have never been great at presentation. I am not the type of developer who knows how to put together an impressive, eye-popping demonstration of the technology I develop. I write libraries, and then I hope that other people can do impressive things with them. Actually showing off the features of my work is something I struggle with; you have probably seen me post console output a lot, and that's because it's all I can bring myself to do. I love coming up with algorithms and weird tools for performing tasks. However, I hate actually applying them to things. As soon as I do, I find that my focus strays away from making my code work for a variety of purposes, and more towards adapting it to whatever I use it in. So I write libraries and release them. I find it more fun to look at what others do with my work than to actually use it myself. It's like an author reading their own books... it's not quite as enjoyable as it would be from another perspective. I wrote it, so there's no sense of wonder, no desire to experiment. I wish I could make my posts more interesting for people who are not as familiar with my projects or backend development in general. It's hard to explain the things I do without, as you said, writing a huge post about the intricacies. But as long as someone understands and appreciates the effort that goes into what I do, I'll post about it. It makes the lonely work worth it.[/QUOTE] I seem to lose interest in projects once I've done all the leg work, or I get to the point where the fun of developing the back-end ends, and the tedious work of making use of it begins. I've written tonnes of parsers, lexers, OpenGL interfacing code, and even the back-end for a interfacing with the Windows Speech API, but the second I had to put it to use, make a language, come up with a game idea, or integrate the speech recognition into my life through voice controlled music players, I just kind of lost interest in it. Once in a full moon I'll maintain momentum long enough to use it, but it's a very rare occurence. [B]edit: [/B] Oh crap, page king, uh.. Directional Lighting! [img]http://i.imgur.com/BqojKAL.png[/img]
Starting on a WPF app for my word vomit library just for some practice, what do you all think of the direction i'm going so far? [IMG]http://i.imgur.com/Zs6sn30.png[/IMG] edit: shit i already see my window controls aren't even vertically centered in the title bar.
[QUOTE=reevezy67;47495700]You're all idiots. [code] Console.WriteLine("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811"); [/code][/QUOTE] Christ, that's verbose. Here's a better version: [quote]0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811[/quote]
you're all wrong, the stackoverflow method is correct [IMG]http://i.imgur.com/K3dgarm.png[/IMG] [IMG]http://i.imgur.com/EUEoTcF.png[/IMG] [IMG]http://i.imgur.com/X0wthcI.png[/IMG] [IMG]http://i.imgur.com/DkWxKif.png[/IMG] joke is old now
[QUOTE=TheEyes;47496400]you're all wrong, the stackoverflow method is correct [IMG]http://i.imgur.com/K3dgarm.png[/IMG] [IMG]http://i.imgur.com/EUEoTcF.png[/IMG] [IMG]http://i.imgur.com/X0wthcI.png[/IMG] [IMG]http://i.imgur.com/DkWxKif.png[/IMG] joke is old now[/QUOTE] except mine is better and i posted it before you :| [img]https://feen.us/cwum9u.png[/img]
Print 100 to 1 with a loop that starts like this: "for (i = 0;". Pick any language, but the loop has to start from i = 0.
[QUOTE=adnzzzzZ;47496433]Print 100 to 1 with a loop that starts like this: "for (i = 0;". Pick any language, but the loop has to start from i = 0.[/QUOTE] [code] for(i = 0; i != 0; ;); for(i = 100; i > 0; i--) print(i); [/code] :smug:
Sorry, you need to Log In to post a reply to this thread.