Jump to content

Custom Sigma method

Had the craving to learn some more algorithms from my MIT book. Finished the first three chapters. Got tired of sigma by paper for things, wanted a fast way to compute for n-elements of array(n)

 

so ya, that's it. with instructions.

From the album:

People That are Cool and I like to talk to

  • 12 images
  • 0 comments
  • 39 image comments


Recommended Comments

Hmmm? You mean 'sigma' as in 'summation' right? There is a built in method for that. It think it's only for later Ruby versions though. You can also do it using inject/reduce.

 

Also: Passing the function as a string to be evaluated? Tsk tsk... That's a bad practice ya know? There is a better way... Blocks are your friend!

 

Another helpful message from Kayzee the programing snob! :3

 

 

 

 

Edited by Kayzee

Share this comment


Link to comment

I am not sure I ever heard it be called a 'sigma method' before, though it makes a degree of sense I guess. But yeah, that's what inject is for more or less.

 

Edited by Kayzee

Share this comment


Link to comment

I mean that if you want to sum an array like that in ruby you usually do something like this:

[1,2,3,4,5].inject(0) {|sum, n| sum+n }

 

Unless what you really want to do are limits?

Edited by Kayzee

Share this comment


Link to comment

I honestly don't really understand what you are doing there then... I guess looking at it, it looks like you are just looking at the array's size and not it's contents. Huh... f(j) = O(n^2) you say? Are you perhaps doing a rough calculation of how a function's time/memory requirements scale with it's input? Actually... Isn't that a type of limit?

Share this comment


Link to comment

basically all the algorithms I learned for first two chapters involve arrays, sorting, searching. And wanted to see how a particular method was compared to another, based on the book's array suggestions

Share this comment


Link to comment

Hehe... Oh I see! I guess I was mistaken then. I am sorry if I was a bother. Though I still likely would have used blocks and not eval... But eh.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×