On-Line Encyclopedia of Integer Sequences

Posted on September 14, 2005

Often in your homework you are given a sequence of numbers and asked to find a formula that produces this sequence.
For example determine the final value (an exact value as a function of n) of sum after executing the following code segment:

        int sum = 0;
        n = n - (n % 2);
        for (int k = 1; k < n; k += 2) {
            int j = 0;
            while (j < k) {
                ++sum;
                ++j;
            } // while
        } // for
        return sum;

This code produces sequence of repeated roots 0,0,1,1,4,4,9,9,16,16,25,25,36,36,49,49,64,64,81,81,100,100.
Now if you can’t find a formula, you can go to On-Line Encyclopedia of Integer Sequences and see that the formula is simply f(n)=[floor(n/2)]^2

Filed Under Uncategorized |

4 Comments so far
  1. Skrud September 15, 2005 12:38 pm

    Wow! Thanks for that! Infinitely useful.

  2. Smokinn September 16, 2005 12:55 am

    Sure, you *could* code it that way but everything just looks so much better in scheme =)

    (define (testCases nbTries)
    (cond
    ((= nbTries 0) (list (integerSequence nbTries)))
    (else (append (testCases (- nbTries 1)) (list (integerSequence nbTries))))
    )
    )

    (define (integerSequence n)
    (* (/ (- n (mod2 n)) 2) (/ (-n (mod2 n)) 2))
    )

    (define (mod2 n)
    (cond
    ((> n 1) (mod2 (- n 2)))
    ((= n 1) 1)
    (else 0)
    )
    )

    There’s probably a better way to write the mod function since this one wouldn’t scale very well but for my needs it was good enough. =)

    Now when you run this:
    > (testCases 21)
    (list 0 0 1 1 4 4 9 9 16 16 25 25 36 36 49 49 64 64 81 81 100 100)

    I was bored and needed to practice my scheme. =/

  3. Iouri Goussev September 16, 2005 2:13 am

    Yes scheme looks nice, challenging to read at first, but elegant.

    I really wanted to take “COMP 348: Principles of Programming Languages” this semester but tutorials conflicted with my other classes. I will take it next semester for sure.

    BTW smokinn can you fix your blog? I couldn’t add your RSS to bloglines, and comments don’t work.

  4. Smokinn September 16, 2005 10:29 am

    I knew RSS didn’t work but I didn’t know about the comments… I should have some free time saturday afternoon and sunday so I’ll try to see what’s going about RSS then. I’ve already fixed the comments.

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name

Email

Website

Comments

For spam detection purposes, please copy the number 7125 to the field below:

© Copyright 0xDEADBEEFCAFE • Powered by Wordpress • Design by Sebastin.

free web hit counter