Thread: problem with while loops

  1. #31
    Registered User
    Join Date
    Jun 2005
    Thank you all so much for your help!!! It's really been very helpful... and I finally got my entire program to work!!

  2. #32
    Join Date
    Jul 2005
    You have a semicolon on the line if (counter % 2 != 0); which means that squareSum = squareSum + (counter * counter); falls after the if statement. It's best to always use braces.

    The following doesn't really help you learn programming, but the sum of the first n odd squares can be computed efficiently with
    int sum_first_n_odd_squares = (n * (4*(n-1)*(n+1) + 3)) / 3;
    so that if n were 5, sum_first_n_odd_squares would be equal to 1*1 + 3*3 + 5*5 + 7*7 + 9*9.

    Using this formula, you could write
    int x = (firstNum + 1) / 2;
    int y = secondNum / 2;
    int oddsq_sum = (y * (4*(y-1)*(y+1) + 3) - x * (4*(x-1)*(x+1) + 3)) / 3;
    I guess the moral is that instead of spending a minute writing a while loop, you could spend five minutes inventing a formula!

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    The problem is already fixed.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #34
    Join Date
    Mar 2005
    Dude so much easier with a loop... And takes no time at all... Save math for the fun stuff...

  5. #35
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >instead of spending a minute writing a while loop, you could spend five minutes inventing a formula!
    We're not all math majors, you know. In fact, my knowledge of mathematics is iffy at best, and that's pretty common. So, I'd much rather spend a minute writing a loop than days figuring out how to invent a formula.
    My best code is written with the delete key.

  6. #36
    Registered User major_small's Avatar
    Join Date
    May 2003
    I'd have to agree with prelude... some things are just soo much easier when you let the computer figure them out for you :P
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 01:09 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM