Pascal - absolute sum of digits

In summary, the program has a problem where it won't work for certain numbers if they have more than one digit. The code has two loops, one to add up the digits and one to check if the sum is greater than zero. If the sum is greater than zero, the second loop is executed, but if the sum is not greater than zero, the first loop is executed.
  • #1
tawi
33
0

Homework Statement


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.
Example:
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.

Homework 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.

The Attempt at a Solution


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

begin
readln(system);
readln(N);

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

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

writeln(result);

end.

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:
Physics news on Phys.org
  • #2
sum := (sum + x);
You haven't run this code yet, have you? You can see sum is an uninitialised variable here.
 
  • #3
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.
 
  • #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.
 
  • #5
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.
 

What is Pascal - Absolute Sum of Digits?

Pascal - Absolute Sum of Digits is a mathematical concept that involves finding the sum of the absolute values of the digits in a given number. It is named after the French mathematician Blaise Pascal, who made significant contributions to the field of mathematics.

How is the Absolute Sum of Digits calculated?

The Absolute Sum of Digits is calculated by adding up the absolute values of each individual digit in a number. For example, the Absolute Sum of Digits for the number 123 would be 1 + 2 + 3 = 6.

What is the significance of Pascal - Absolute Sum of Digits?

Pascal - Absolute Sum of Digits is used in various mathematical concepts and formulas, such as the Pascal Triangle and the Pascal Matrix. It also has applications in computer science, particularly in error detection and correction algorithms.

Can the Absolute Sum of Digits be negative?

No, the Absolute Sum of Digits cannot be negative. The absolute value of a number is always positive, so the sum of these values will also be positive.

How is Pascal - Absolute Sum of Digits related to other mathematical concepts?

Pascal - Absolute Sum of Digits is closely related to other mathematical concepts such as digital roots, which also involve manipulating and analyzing the digits of a number. It is also related to the concept of absolute value, which is used to find the distance between a number and zero.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
4
Views
184
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
4
Views
908
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
Back
Top