Recursive Algorithm for CHKSUM Calculation: div and mod Functions | n >= 0

  • Thread starter teng125
  • Start date
In summary: The sum of the digits of 19 is 1 + 9 = 10.In summary, the algorithm for CHKSUM(n) is to take the modulus of n by 10 to get the rightmost digit, then subtract that digit from n and divide by 10. This process is repeated until n becomes 0, and the sum of all the digits is calculated. This algorithm uses the div function and mod function.
  • #1
teng125
416
0
pre n >= 0
post checksum(n) = CHKSUM(n)
reads n
changes -
mem -

Remark: Let n be an integer number. Furthermore, CHKSUM(n) = “sum of digits of n”. checksum is the name of the module to be developed.

Develop a recursive algorithm for the above specification. Assume and use functions
div function and mod function.

can smby pls show me how to write this algorithm

thanx
 
Physics news on Phys.org
  • #2
Well first off, you need to know exactly what your algorithm is supposed to do. Looks like your CHKSUM(n) is supposed to find the sum of the digits of n. So, take an example. If you have n = 198...then checksum should return what value?

After that it should be pretty straightforward, if you know what value it's supposed to be.
 
  • #3
does it means sum up all the values from 1 to 198 if n=198??
actually i don'tknow what does the algorithm above means at all

why does it needs to use functions div function and mod function.

if possible,pls explain to me

thanx
 
  • #4
No it does not mean that...it means to sum up the "digits" that make up the number 198. So the number 198 is made up of 3 "digits"...which numbers and what is their sum?
 
  • #5
pls show me how to write this recursive algorithm.I need it very much,pls

thanx
 
  • #6
We're not going to do your problem for you.

The checksum of 198 should be 1 + 9 + 8 = 18.
Here is another hint: 198 mod 10 = 8.
(198 - 198 mod 10) / 10 = 19
 

1. What is the purpose of a checksum in scientific research?

The purpose of a checksum in scientific research is to detect errors or changes in data. It is a mathematical algorithm used to calculate a unique value for a set of data, which can then be compared to the original value to ensure that the data has not been altered in any way.

2. How is a checksum calculated?

A checksum is calculated by using a specific algorithm, such as CRC or MD5, to generate a unique value for a set of data. This value is typically much smaller than the original data, making it easier to compare and detect any changes or errors.

3. What are the advantages of using checksums?

Checksums have several advantages in scientific research, including the ability to quickly detect errors or changes in data, ensuring data integrity, and providing a way to verify the authenticity of data.

4. Can a checksum be used to fix errors in data?

No, a checksum is not designed to fix errors in data. Its purpose is to detect errors and alert researchers to potential issues with their data. In some cases, it may be possible to use checksums to identify and correct errors, but this is not their primary function.

5. Are checksums always reliable?

While checksums can be a useful tool for detecting errors in data, they are not 100% reliable. It is possible for two different sets of data to have the same checksum, known as a "collision." However, the likelihood of this occurring is extremely low, and checksums are still considered a reliable method for ensuring data integrity.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
995
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • Math Proof Training and Practice
3
Replies
86
Views
19K
  • Math Proof Training and Practice
2
Replies
60
Views
8K
  • Math Proof Training and Practice
2
Replies
52
Views
9K
Back
Top