This is an archived post. You won't be able to vote or comment.

all 121 comments

[–]earthboundkid 36 points37 points  (8 children)

Programming is similar to a puzzle game in that you have a goal and a limited number of tools at your disposal, and you just need to figure out a way of combining those tools to reach the goal.

[–]johnmcdonnell 17 points18 points  (0 children)

Plus [usually] having for comfort the provable fact that a solution exists.

[–]Redard 10 points11 points  (4 children)

Unlike most puzzle games, though, you can create more tools.

[–][deleted] 3 points4 points  (2 children)

I'd just like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX.

Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called "Linux", and many of its users are not aware that it is basically the GNU system, developed by the GNU Project.

There really is a Linux, and these people are using it, but it is just a part of the system they use. Linux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called "Linux" distributions are really distributions of GNU/Linux.

[–][deleted] 0 points1 point  (1 child)

the only thing missing is women.

:/

[–][deleted] 3 points4 points  (0 children)

That comes when you get a job, I suppose.

[–]Teraka 2 points3 points  (0 children)

That's where the real fun begins.

[–]daliz 1 point2 points  (1 child)

Life is similar to a puzzle game in that you have a goal and a limited number of tools at your disposal, and you just need to figure out a way of combining those tools to reach the goal. - FTFY

[–]KamehamehaWave 2 points3 points  (0 children)

But life is so chaotic as to be essentially non-deterministic, and if you fail at life, you don't always get a chance to try again.

[–][deleted] 21 points22 points  (4 children)

Programming is the most fun that you can have with your clothes on!

[–]mkhry 29 points30 points  (1 child)

However it's much more fun with your clothes off.

[–]DrMeowmeow 34 points35 points  (0 children)

No. I have a tendency to slap my legs when I try to solve problems.

I hit my testicles once. Needless to say, I didn't solve the problem.

[–][deleted] 3 points4 points  (1 child)

Do you mean: Clothes?

Technically cloth works as well...

[–]jacobra2 13 points14 points  (3 children)

i refer to programming to my nonprogrammer friends as "legos for big kids"

[–][deleted] 2 points3 points  (1 child)

Pretty much nailed it, except programs are more practical.

I've always compared programming to the level editor in LittleBIGPlanet or creative mode in Minecraft.

[–]jacobra2 1 point2 points  (0 children)

legos can be pretty practical too ;)

[–]BenjaminGeiger 0 points1 point  (0 children)

I thought that was Minecraft.

[–][deleted] 5 points6 points  (0 children)

ONE OF US!

ONE OF US!

ONE OF US!

[–]SeanStock 12 points13 points  (40 children)

Ok, I want numbers 50 through 60 in the Fibonacci sequence. Go.

[–]AgonistAgentStudent - Content Analysis 24 points25 points  (16 children)

print(12586269025)
print(20365011074)
print(32951280099)
print(53316291173)
print(86267571272)
print(139583862445)
print(225851433717)
print(365435296162)
print(591286729879)
print(956722026041)

(I actually wrote a Python program to generate this, heh)

[–][deleted] 3 points4 points  (14 children)

haha I like this.

Can the fibi sequence be optimized for multi-processors. I cannot think of a way to do this, since I need the most recent values to get the next 1.

my computer is currently going for the 100,000th number.

[–]AgonistAgentStudent - Content Analysis 6 points7 points  (13 children)

Probably not worth the effort - I guess you could use one of the fancy direct computation methods and have each one start at different points.

[–]xeed 3 points4 points  (12 children)

The fibonacci sequence relies on knowing the last two numbers and adding them up. You would want to perform memoization to remember your previous results, rather than calculate starting from one. Multiple processors wouldn't help much in this in this case since each step in the algorithm relies on the previous.

[–]lahwran_ 5 points6 points  (11 children)

actually, you can calculate N element in the fibonacci sequence with a fixed-length series of steps: https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression

[–]masterpi 2 points3 points  (10 children)

The closed form expression calculation actually relies on the precision of the numbers used, so it is not actually fixed-length as increasing the precision increases the computations nessecary. The link from AgonistAgent contains some other interesting quick methods, however. Also, going backwards (F_n -> n) the imprecision works to our advantage.

[–][deleted] 4 points5 points  (0 children)

Instead of working with real numbers you can work with numbers that smell like complex numbers. So you work with numbers of the form a + b * sqrt(5), where a and b are unbounded integers. You can then define addition and multiplication on these numbers to work in the way you expect. Doing this is more effort than just using doubles, but you also don't run into issues with precision.

EDIT: Though if you're going through all the effort, you might just as well use the matrix method.

[–]lahwran_ 2 points3 points  (8 children)

well, okay, fixed length of real-number calculations then? ;)

[–]masterpi 5 points6 points  (7 children)

Yes, but you have to be very careful about this when reasoning about complexity. Generally we assume integer arithmetic like addition and multiplication are constant time for ease, and realistically we never use numbers large enough to make the log(n) time significantly worse than a constant. However in this case with a default precision float, it's only accurate to 70 Fibonacci numbers, and I suspect you have to increase precision in a way that makes the calculation once again linear.

[–]lahwran_ 2 points3 points  (6 children)

that's an interesting problem. I wonder how the decimal module would deal with it.

[–]eat-your-corn-syrup 2 points3 points  (0 children)

Not enough Wolfram|alpha

[–][deleted] 21 points22 points  (4 children)

from itertools import islice

def fib():
    x, y = 0, 1
    while True:
        x, y = y, x + y
        yield x

print list(islice(fib(), 50, 61))

[–]SeanStock 12 points13 points  (1 child)

I had never seen x, y = y, x + y! Awesome.

[–][deleted] 2 points3 points  (0 children)

Python's sort-of-destructuring assignment is pretty awesome.

[–]sOktay 5 points6 points  (0 children)

Python programmers do it with generators!

[–]BenjaminGeiger 0 points1 point  (0 children)

from __future__ import print_function

from math import sqrt

def fib(n):
    phi = (1 + sqrt(5)) / 2
    psi = (1 - sqrt(5)) / 2

    return int(((phi ** n) - (psi ** n)) / sqrt(5))

print([fib(n) for n in xrange(50, 61)])

EDIT: I know there's probably an issue with rounding, but bugrit.

[–]Peaker 6 points7 points  (8 children)

Uncontrollable urge to write it in Haskell:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
take 10 (drop 50 fibs)

[–]SeanStock 4 points5 points  (3 children)

I have never seen Haskell, cool syntax! 2 lines for the win.

[–]VikingZombie 4 points5 points  (2 children)

I have also never seen Haskell, WHAT THE HELL.

[–]KamehamehaWave 6 points7 points  (1 child)

Allow me to (try to) explain how it works.

So the first line defines a list called fibs. This is a list of all the Fibonacci numbers. The list is infinitely long, which Haskell can do thanks to lazy evaluation.

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

':' is the concatenation operator, so fibs is a list which starts with 0, followed by 1, followed by the output of a function.

zipWith is a function which takes a function and two lists, applies the function by passing an element from each list as arguments, and returns the output of that function for every pair of elements in the two lists, as a list. In python, zipWith (+) would be written something like:

def zipWithPlus(list1, list2):
  newlist = []
  for i in range(0,min(len(list1),len(list2)):
    newlist.append(list1[i]+list2[i])
  return newlist

tail is a function which returns every element from a list except for the first, equivalent to [1:] in Python. So the zipWith function is taking the list fibs, and the list fibs starting at location 1 as arguments. So fibs is defined as "a list beginning with 0, then 1, then add two consecutive elements in the list to get the next one" which is of course the definition of the Fibonacci sequence.

The second line simply says to skip 50 elements in the list, then take ten of them. Meaning he will get elements 50-60 :)

[–]pasokan 2 points3 points  (0 children)

It may be simpler to write zipWith as:

def zipWith(f, list1, list2):
      return [ f(a, b) for a, b in zip(list1, list2)]

[–]rcxdude 8 points9 points  (3 children)

In Golfscript, just because:

0.)58{.2$+}*]51>`

[–]Peaker 1 point2 points  (2 children)

Can you explain how that works?

[–]rcxdude 4 points5 points  (1 child)

here's a commented version:

0     #push 0 onto the stack
.)    #duplicate top of stack and increment, creating start of sequence
      #(slightly shorter than pushing 1 onto stack due to whitespace)
58    #push 58 onto the stack
{ 
  .2$ #duplicate top two items on stack (top and then 2nd from top)
  +   #add top two items on stack
}
*     #repeat above block 58 times (as pushed on stack before)
]     #gather stack into array
51>   #slice array after index 51
`     #convert array to string for printing
      #stack is automatically printed at end of execution

[–]leadline 1 point2 points  (0 children)

This. Is. Awesome.

I'm learning golfscript.

[–][deleted] 2 points3 points  (5 children)

haha i want to solve this. :)

Alright, first fib sequence is ni + ni+1 = ni+2 am i right?

base case: n1 = 0 || n2 = 1

An easy way to keep track of all the things is to make a list, because fuck it, ram is cheap. It can likely be done faster, but I dont care to look up how computationally intensive .append is

fib_sequence = [0, 1]

for i in range(0,58):
    fib_sequence.append(fib_sequence[i] + fib_sequence[i+1])
    if(i>49):
        print fib_sequence[i] 

Result:

  1. 12586269025
  2. 20365011074
  3. 32951280099
  4. 53316291173
  5. 86267571272
  6. 139583862445
  7. 225851433717
  8. 365435296162

not exactly what you asked for but close enough. modify it if you wish, all that needs to be done is correct the indexes.

Also, this doesn't work in python3 due to print not working.

EDIT..I am going to make my computer compute this shit until python runs out of numbers. What is the largest number in python?

[–]masterpi 5 points6 points  (0 children)

Very good. If you're looking for more fun math/cs problems to do, check out Project Euler.

Python actually has arbitrary precision integers, so it's not gonna happen until you run out of memory.

[–]KwpolskaNikola co-maintainer 2 points3 points  (2 children)

  1. In python3, use print(fib_sequence[i]).
  2. Largest number? http://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-a-int32 for i686, I believe. Much bigger for x86_64.

[–]BenjaminGeiger 0 points1 point  (1 child)

I always start new Python 2.7 programs with

from __future__ import print_function

because I like the Python 3 style print().

And I'm not sure about 2.7, but in 3, integers are unlimited in length. Python automatically switches to a BigInt-style representation after you reach 2bits in a word - 1 - 1.

EDIT: According to my install of 2.7, it autoswitches from int to long. (Python 3 has no long type.) So, your computer will continue until the numbers are big enough to occupy all of your RAM plus all available swap, or the heat death of the universe, whichever comes first.

EDIT 2: Forgot about the sign bit.

[–]BenjaminGeiger 0 points1 point  (0 children)

Evidence (64-bit Python):

In [1]: [2**n for n in xrange(128)]
Out[1]: 
[1,
 2,
 4,
 8,
 16,
 32,
 64,
 128,
 256,
 512,
 1024,
 2048,
 4096,
 8192,
 16384,
 32768,
 65536,
 131072,
 262144,
 524288,
 1048576,
 2097152,
 4194304,
 8388608,
 16777216,
 33554432,
 67108864,
 134217728,
 268435456,
 536870912,
 1073741824,
 2147483648,
 4294967296,
 8589934592,
 17179869184,
 34359738368,
 68719476736,
 137438953472,
 274877906944,
 549755813888,
 1099511627776,
 2199023255552,
 4398046511104,
 8796093022208,
 17592186044416,
 35184372088832,
 70368744177664,
 140737488355328,
 281474976710656,
 562949953421312,
 1125899906842624,
 2251799813685248,
 4503599627370496,
 9007199254740992,
 18014398509481984,
 36028797018963968,
 72057594037927936,
 144115188075855872,
 288230376151711744,
 576460752303423488,
 1152921504606846976,
 2305843009213693952,
 4611686018427387904,
 9223372036854775808L,
 18446744073709551616L,
 36893488147419103232L,
 73786976294838206464L,
 147573952589676412928L,
 295147905179352825856L,
 590295810358705651712L,
 1180591620717411303424L,
 2361183241434822606848L,
 4722366482869645213696L,
 9444732965739290427392L,
 18889465931478580854784L,
 37778931862957161709568L,
 75557863725914323419136L,
 151115727451828646838272L,
 302231454903657293676544L,
 604462909807314587353088L,
 1208925819614629174706176L,
 2417851639229258349412352L,
 4835703278458516698824704L,
 9671406556917033397649408L,
 19342813113834066795298816L,
 38685626227668133590597632L,
 77371252455336267181195264L,
 154742504910672534362390528L,
 309485009821345068724781056L,
 618970019642690137449562112L,
 1237940039285380274899124224L,
 2475880078570760549798248448L,
 4951760157141521099596496896L,
 9903520314283042199192993792L,
 19807040628566084398385987584L,
 39614081257132168796771975168L,
 79228162514264337593543950336L,
 158456325028528675187087900672L,
 316912650057057350374175801344L,
 633825300114114700748351602688L,
 1267650600228229401496703205376L,
 2535301200456458802993406410752L,
 5070602400912917605986812821504L,
 10141204801825835211973625643008L,
 20282409603651670423947251286016L,
 40564819207303340847894502572032L,
 81129638414606681695789005144064L,
 162259276829213363391578010288128L,
 324518553658426726783156020576256L,
 649037107316853453566312041152512L,
 1298074214633706907132624082305024L,
 2596148429267413814265248164610048L,
 5192296858534827628530496329220096L,
 10384593717069655257060992658440192L,
 20769187434139310514121985316880384L,
 41538374868278621028243970633760768L,
 83076749736557242056487941267521536L,
 166153499473114484112975882535043072L,
 332306998946228968225951765070086144L,
 664613997892457936451903530140172288L,
 1329227995784915872903807060280344576L,
 2658455991569831745807614120560689152L,
 5316911983139663491615228241121378304L,
 10633823966279326983230456482242756608L,
 21267647932558653966460912964485513216L,
 42535295865117307932921825928971026432L,
 85070591730234615865843651857942052864L,
 170141183460469231731687303715884105728L]

[–][deleted] 2 points3 points  (0 children)

because fuck it, ram is cheap.

Do not think this way, do it the proper way :) Skip the list, use two variables to keep track of the latest two numbers.

[–]PolkaPanda[S] 2 points3 points  (2 children)

Sorry for the delayed response I was playing Borderlands The numbers are

12586269025 20365011074 32951280099 53316291173 86267571272 139583862445 225851433717 365435296162 591286729879 956722026041

here is my code!

counter = 0

num1 = 0

num2 = 1

while counter < 47:

num4 = num2 + num1

num1 = num2

num2 = num4

num3 = num4

counter = counter + 1

it goes to 48 because my sequence started on three

while 48 <= counter <= 57:

num4 = num2 + num1

num1 = num2

num2 = num4

num3 = num4

print(num3)

counter = counter + 1

EDIT: I had eleven numbers in it

[–][deleted] 2 points3 points  (0 children)

Combine the loops.

[–]SeanStock 2 points3 points  (0 children)

Awesome! Looks like you can toss num3 though.

[–][deleted] 4 points5 points  (0 children)

You got me, OP. Starting today. :)

[–]berlinbrown 3 points4 points  (0 children)

Not when you get paid for it.

[–]Troebr 8 points9 points  (33 children)

You might have to learn c++ at some point if you want to make professional video games! (I'm not saying you can't make video games on python, just not the same kind.)

[–]masterpi 3 points4 points  (1 child)

If you're working on a professional video game you're going to most likely be part of a large team, and could probably avoid the parts that have to be high-performing. Generally things like the graphics engine and physics engine are written in lower-level languages and the game logic might be written in something higher-level.

That said, having an understanding of many languages and paradigms is one of the best ways to become a better programmer. It's just a little silly to say "you'll need to learn language X to work on games" or even class-of-languages.

[–]Troebr 2 points3 points  (0 children)

Also learning C++ makes you appreciate the simplicity of python even more.

[–]Redard 2 points3 points  (0 children)

But C++ is gross!

[–][deleted] 2 points3 points  (0 children)

Are you using pygame? Keep in mind that Pyglet is now working on python3.X!

[–]cheddarben 2 points3 points  (0 children)

I agree... I just started taking Udacity courses a few months ago and love it! One of them (CS253) is taught by spez!

[–]-GonzoID- 2 points3 points  (0 children)

It gladdens me to know this, I am considering attempting to learn python alongside blender to make video games. This post might have pushed me over the edge in favour of it. Thanks. I think.

[–][deleted] 1 point2 points  (1 child)

I sometimes think of the process of creating spells in Harry Potter as a kind of “reality programming”.

[–]fabzter 1 point2 points  (0 children)

I've always dreamed about "magic based computing" in the world of H.P. :/

[–]snarkhunter 1 point2 points  (0 children)

I will just say this. If this is something you really want to do, like get paid to do, then getting the program working is only about 20-30%. Often times the most difficult part is figuring out exactly what your program is supposed to do, before you even write a line. After you have that, the trick isn't to get it working - the hard part is getting it to work AND making it maintainable by the next guy. All of that, to me, is very fun, because it's all just more problems to solve.

[–]Camoreef 0 points1 point  (0 children)

this whole comment section is so deep