Thread: The sum of squared digits

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    The sum of squared digits

    I currently want to use the 7 digits: 1234567 to make the sum of squared digits so 1^2+2^2+3^2+4^2+5^2+6^2+7^2=140

    I have already done this code as follows:
    b = (id/1000000)^2 + ((id%1000000)/100000)^2 +
    ((id%100000)/10000)^2 + ((id%10000)/1000)^2 +
    ((id%1000)/100)^2 + ((id%100)/10)^2 + (id%10)^2;

    b should equal to 140, but I only get the sum of 2. Can somebody please help me.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    a^b is not power in C/C++, it's a bitwise operation XOR
    Read this:
    bitwise opearation

    You should use function pow() declared in cmath header
    Last edited by Micko; 04-10-2005 at 03:17 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Is this what you mean?
    #include <cmath>

    I have done this as follows:
    b = (id/1000000)pow(1,2) + ((id%1000000)/100000)pow(1,2) + ((id%100000)/10000)pow(1,2) + ((id%10000)/1000)pow(1,2) + ((id%1000)/100)pow(1,2) + ((id%100)/10)pow(1,2) + (id%10)pow(1,2);

    I think is wrong, so can you please give me an example. I am new to c++ so I need to look at some example so I can get a better undstanding. Thanx.

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    OI! Ease up on the double posting

    (other thread)

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of digits of an integer: Odd or Even?
    By Devolution in forum C Programming
    Replies: 14
    Last Post: 03-06-2009, 06:24 PM
  2. Help on Sum of Digits Entered
    By SkinnyK in forum C Programming
    Replies: 5
    Last Post: 05-14-2008, 05:16 AM
  3. Need Help Writing a Sum of Digits program
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 03-13-2008, 03:06 AM
  4. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  5. Replies: 7
    Last Post: 05-26-2003, 05:44 PM