Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 489 490 [491] 492 493 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 903637 times)

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7350 on: May 12, 2015, 10:44:08 am »

I don't know if that formula is the fastest... trying to actually find the Nth fibbonacci number with the formula is probably very imprecise because limited precision for reals and all.

I do know that trading memory for speed is a thing... not sure if it's applicable here! I would use the naive application but also have a dict with all the already found fibbo numbers.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7351 on: May 12, 2015, 11:57:50 am »

Well, all Fibonacci numbers are natural numbers so you could probably just wrap that in a round().
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #7352 on: May 12, 2015, 01:35:27 pm »

I'm trying to store and return values for the Balance and Owner variables. It throws an error saying the variables are already declared, but I can't get rid of the variables at the module level (can I?). What do?

Code: [Select]
Public Class ClassAccount

    Public Property Balance As Decimal
    Private modBalance As Decimal
    Public Property Owner As String
    Private modOwner As String

    Public Function Balance() As Decimal
        Get
            Return = BalanceValue
        End Get
    End Function

    Public Function Owner() As String
        get
            Return = OwnerValue
        End get
    End Function

End Class

Also I'm not sure if those Get-Set functions will work the way I want them to.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7353 on: May 12, 2015, 02:30:32 pm »

Here's how to logarithmic Fibonacci:

Code: [Select]
[int, int] fib2 (int n) {
// Returns the (n-1)st and nth Fibonacci numbers
if (n = 1) {
return [0, 1];
}
if (n <= 0) {
[int a, int b] = fib2(1-n);
int p = 1-2*(n%2);
return [p*b, -p*a];
}
[int a, int b] = fib2(n/2); // Integer division
if (n%2 == 0) {
return [a*a+b*b, b*(a+a+b)];
} else {
return [b*(a+a+b), b*b+(a+b)*(a+b)];
}
}

// For completeness' sake
int fib (int n) {
[int a, int b] = fib2(n);
return b;
}

@Gunner-senpai: Wtf is that programming language?
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7354 on: May 12, 2015, 03:06:59 pm »

@Gunner-senpai: Wtf is that programming language?

I'm not OP, but it looks like VB.NET.
« Last Edit: May 12, 2015, 03:10:30 pm by Mephisto »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7355 on: May 12, 2015, 03:08:17 pm »

Yeah, VB.NET. For some reason, it's pretty popular, especially among Dwarf Fortress applications (see: Masterwork UI, Raw Explorer, old LNP IIRC...)

Parsely

  • Bay Watcher
    • View Profile
    • My games!
Re: if self.isCoder(): post() #Programming Thread
« Reply #7356 on: May 12, 2015, 04:03:16 pm »

It's VB, coded in Visual Studio.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7357 on: May 12, 2015, 05:33:26 pm »

Code: [Select]
int fibonacci(int n)
{
    if(n>1)
        return fibonacci(n-1) + fibonacci(n-2);
    else if(n==1)
        return n;
    else
        return 0;
}

This only works for n < 40something, if I remember right.
Not saying that it is wrong, just keep in mind how a small n can produce a rabitsplosion the likes of which even Armok has never seen.
Well, a recursive solution was asked for. Exploding the stack was not specified as an issue.

It's clear since each iteration calls the ones before it twice, that +1 n will double the runtime. The above runs in O(2n) time.

Running tests, it's the slowdown that kills the recursive version around #40, and the linear time version dies after #92, because that's where 64-bit numbers are no longer sufficient to hold the number, and the number is forced to be negative. If you want to get up to fibonacci #100, special tactics are required ;D
« Last Edit: May 12, 2015, 06:03:54 pm by Reelya »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7358 on: May 12, 2015, 07:46:15 pm »

If you want to get up to fibonacci #100, special tactics are required ;D

Memoization and a language that supports huge numbers?

With some simple memoization and increasing the system recursion limit, Python 3 does fib(5000) in a few thousandths of a second.
« Last Edit: May 12, 2015, 07:50:17 pm by Mephisto »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7359 on: May 12, 2015, 08:18:01 pm »

If you're going to use memoization why not just store the entire thing in a lookup table and get the result in a nanosecond.

But to get the memoization table going you do need to compute the values once right? If you're not doing that, you're just importing a lookup table with precalculated values, so it's not the same problem any more.
« Last Edit: May 12, 2015, 08:21:23 pm by Reelya »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7360 on: May 12, 2015, 08:21:18 pm »

the entire thing

can you make a lookup table of countably infinite size

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7361 on: May 12, 2015, 08:21:49 pm »

Can you hold a memoization table of countably infinite size? no.

The memoization table is better than just the recursion by itself. Since it will reduce the main algorithm it to linear time. But doing without the recursion is clearly better in any case.
« Last Edit: May 12, 2015, 08:31:51 pm by Reelya »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7362 on: May 12, 2015, 08:25:35 pm »

Is there a number that you can input into this function that will result in a memoization table of countable infinite size?

wait what the hell are we doing here

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7363 on: May 12, 2015, 08:33:39 pm »

You can always resize a lookup table if the index goes over the top, and use a linear calculation to fill in the blanks. This will be quicker than using the recursive function to do similar.

Anyway you can always grab a class that does big numbers or roll your own in any language. Big numbers is no reason to use Python, the "Duplo" of programming languages.
« Last Edit: May 12, 2015, 08:40:00 pm by Reelya »
Logged

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7364 on: May 12, 2015, 10:44:06 pm »

Simple non-recursive Fibonacci using ring buffer:
Code: (c/c++) [Select]
int fibonacci(int n)
{
int buffer[] = { 2, 1, 1 };

for (int i = 4; i <= n; i++)
buffer[i % 3] = buffer[(i - 1) % 3] + buffer[(i - 2) % 3];

return buffer[n % 3];
}
I think this is the dynamic programming O(n) solution miauw62 mentioned. I didn't bother accounting for invalid input. Had to use this on a final exam I had today, but in pseudocode/Python.

Edit: Simplified to a single return statement.
Edit II: Got rid of redundant 'if' statement. 'For' loop handles it.
Edit III: Coding Standards Be Damned edition:
Code: [Select]
int f(int n){int b[]={2,1,1};for(int i=4;i<=n;i++){b[i%3]=b[(i-1)%3]+b[(i-2)%3];}return b[n%3];}
« Last Edit: May 12, 2015, 11:06:24 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?
Pages: 1 ... 489 490 [491] 492 493 ... 796