Click here to Skip to main content
15,887,822 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Sith Interviewing Tactics Pin
Chris Losinger15-Jul-09 4:49
professionalChris Losinger15-Jul-09 4:49 
GeneralRe: Sith Interviewing Tactics [modified] Pin
_Erik_7-Jul-09 4:15
_Erik_7-Jul-09 4:15 
GeneralRe: Sith Interviewing Tactics Pin
Paulo Zemek7-Jul-09 7:27
mvaPaulo Zemek7-Jul-09 7:27 
GeneralRe: Sith Interviewing Tactics Pin
Dan Neely7-Jul-09 9:14
Dan Neely7-Jul-09 9:14 
GeneralRe: Sith Interviewing Tactics Pin
_Erik_8-Jul-09 5:43
_Erik_8-Jul-09 5:43 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes15-Jul-09 4:33
ProtoBytes15-Jul-09 4:33 
GeneralRe: Sith Interviewing Tactics Pin
Paulo Zemek16-Jul-09 14:14
mvaPaulo Zemek16-Jul-09 14:14 
GeneralRe: Sith Interviewing Tactics [modified] Pin
ProtoBytes7-Jul-09 18:04
ProtoBytes7-Jul-09 18:04 
Okay after much testing I have a non recursive version:

C#
using System;

namespace Test
{
    class fib 
    {
        double n = 0;
        public double next
        {
            get
            {
                return n;
            }

            set
            {
                n = Math.Round(((Math.Pow(fib.golden(),value)) - Math.Pow((1-fib.golden()),value)) / Math.Sqrt(5));
            }
        }

        private static double golden()
        {
            return (1 + Math.Sqrt(5)) / 2;

        }
    }
}

        public static double MyFib(int n)
        {
            fib f = new fib();
            f.next = n;
            return f.next;
        }


Like I said, 'recursive algroythms are a bad idea.'

GoodFib(6000)
-1142292160

MyFib(6000)
Infinity

GoodFib(1000)
1556111435

MyFib(1000)
4.3466557686938915E+208

If you change GoodFib to return a double:

GoodFib(6000)
Infinity

GoodFib(1000)
4.3466557686937428E+208

MyFib looses precision (I think):

C#
public static double NotEqual()
{
    double count;
    double result1 = 0;
    double result2 = 0;
    for (count = 1;; count++)
    {
        result1 = GoodFib(count);
        result2 = MyFib(count);
        if (result1 != Math.Floor(result2))
            break;
    }
    Console.WriteLine("Result1: [" + result1.ToString() + "]");
    Console.WriteLine("Result2: [" + result2.ToString() + "]");
    return count;
}

NotEqual()
71.0
Result1: [308061521170129]
Result2: [308061521170130] ?!?!

Not really sure which is correct. The online sources say GoodFib is right. If I was really to do something like this I would use Math Lab C# extensions. Guarinteed precision.

modified on Wednesday, July 8, 2009 2:42 AM

GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes7-Jul-09 20:53
ProtoBytes7-Jul-09 20:53 
GeneralRe: Sith Interviewing Tactics Pin
_Erik_8-Jul-09 1:54
_Erik_8-Jul-09 1:54 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes8-Jul-09 6:29
ProtoBytes8-Jul-09 6:29 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes8-Jul-09 6:51
ProtoBytes8-Jul-09 6:51 
GeneralRe: Sith Interviewing Tactics [modified] Pin
ProtoBytes8-Jul-09 7:59
ProtoBytes8-Jul-09 7:59 
GeneralRe: Sith Interviewing Tactics Pin
_Erik_10-Jul-09 3:47
_Erik_10-Jul-09 3:47 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes10-Jul-09 5:09
ProtoBytes10-Jul-09 5:09 
GeneralRe: Sith Interviewing Tactics Pin
exyyle21-Jul-09 17:54
professionalexyyle21-Jul-09 17:54 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes21-Jul-09 17:58
ProtoBytes21-Jul-09 17:58 
JokeRe: I think the Interviwer missed my example of recursion Pin
ProtoBytes22-Jul-09 3:54
ProtoBytes22-Jul-09 3:54 
GeneralRe: I think the Interviwer missed my example of recursion Pin
exyyle22-Jul-09 6:52
professionalexyyle22-Jul-09 6:52 
GeneralA real Mess Pin
Bernard Laplace2-Jul-09 1:33
Bernard Laplace2-Jul-09 1:33 
GeneralRe: Swiss made Pin
OriginalGriff2-Jul-09 2:07
mveOriginalGriff2-Jul-09 2:07 
GeneralRe: Swiss made Pin
TommyTomToms2-Jul-09 4:06
TommyTomToms2-Jul-09 4:06 
GeneralRe: Swiss made Pin
Dave Kreskowiak2-Jul-09 4:06
mveDave Kreskowiak2-Jul-09 4:06 
JokeRe: Swiss made Pin
Gautier Boder21-Sep-09 3:54
Gautier Boder21-Sep-09 3:54 
GeneralRe: Swiss made Pin
Thomas Weller2-Jul-09 4:53
Thomas Weller2-Jul-09 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.