1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pascal - absolute sum of digits

  1. Nov 16, 2015 #1
    1. The problem statement, all variables and given/known data
    Make an "absolute sum of digits" of a number in a particular numerical system. You are given two numbers (numerical system and your number N). You take the number N, add up its digits, if the result has more than one digit you add them up again and so on. You end up with one-digit result.
    1) You are given numbers 10, 555. 10 is your numerical system so we are in decimal and you are adding up the digits of 555. So 5 + 5 + 5 = 15 but because it has more than two digits you add them up again and your result is 1 + 5 = 6.
    2) 2, 255. We are in binary and 255 in binary equals to 11111111. We add those ones up and get 8. 8 in binary is 1000, we add that up and 1 will be our output.

    2. Relevant equations
    I´ve made a program that does all this but there is a loophole where, for certain numbers, it won´t work.
    The problem is that, if you look at the code below, there is the second while cycle for sum>0... Take the number 5996 in decimal for instance and go through the first cycle that determines its "first" sum of digits in that particular system... We get a correct result 29. Now go through the second cycle that should continue adding up its digits until sum<=0.
    But if you take 29 through that cycle on the first pass you get: y=9, sum=2, result=9.
    Second pass: y=2, sum=0, result = 11. Our sum is 0 and thus the cycle stops but our result still has two digits and we still need to add those ones up. Obviously there needs to be some different or extended condition for the second while cycle but I don´t know how to achieve what I want.
    Of course I could just put a condition at the end and ad those two digits up separately but what if the same happens and the number has three digits?
    I can´t do (while result > 9) instead of (while sum > 0) because the result starts at zero and goes up so it makes no sense.

    3. The attempt at a solution
    Code (Text):

    program abcd;
    var system, N, x, y, sum, result : integer;


    while N > 0 do
      x := (N mod system);
      N := (N div system);
      sum := (sum + x);

      while sum > 0 do
      y := (sum mod system);
      sum := (sum div system);
      result := (result + y);


    EDIT: hmm, after some thinkink I figured I can just put another while cycle (similar to the first one) at the end that would again simply made a sum of the digits of the result of the second while cycle (result). It doesn´t seem that sophisticated but I guess it should work for most numbers. Exception being some huge number that even after the second cycle and the third cycle still consists of more than one digit. No idea how to go around that.
    Last edited: Nov 16, 2015
  2. jcsd
  3. Nov 16, 2015 #2


    User Avatar

    Staff: Mentor

    You haven't run this code yet, have you? You can see sum is an uninitialised variable here.
  4. Nov 16, 2015 #3


    User Avatar

    Staff: Mentor

    Rather than code multiple separate loops, you should aim ultimately for one concise all-embracing loop, something like

    while (result > max_single_digit) do

    That's really how you'd do it mentally, staying in a loop until the result is a single digit, so that's how you can set the program to do it. But there's no harm in constructing multiple separate loops as you are here as part of preliminary design to get a feel for the code, before then revising it to become one carefully-crafted loop.
  5. Nov 16, 2015 #4
    I have run it and it works as I would expect. Guess I should say sum:=0 and result:=0 at the begining.
    I get what you are saying and I understand that is the preferable outcome but until I can deal with the mistake, where it doesnť work for certain numbers I feel like there is no point in doing that.
  6. Nov 17, 2015 #5


    User Avatar

    Staff: Mentor

    You are saying that the actual number of your "single use" loops needed does vary and is dependent on the data, whereas I am saying if you can combine them into a single versatile loop then execution can stay in that loop as long as it takes until the sum-of-the-digits reduces to a single digit, and you won't care how many times it needs to loop. This means what you are calling a "mistake" would become a non-issue and vanish; so it is well worth your investment of time.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Pascal - absolute sum of digits
  1. Pascal problem (Replies: 1)

  2. Pascal programming (Replies: 1)

  3. Pascal - "nice numbers" (Replies: 21)