![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-08 22:13 |
47,811 topics / 349,895 posts / today 0 new / 48,253 members |
| DOS批处理 & 脚本技术(批处理室) » To find the greatest common divisor (GCD) and the least common multiple (LCM) |
| Printable Version 3,600 / 9 |
| Floor1 namejm | Posted 2007-01-24 13:35 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
I saw that lxmxn was playing with a batch script to find prime numbers, so I also played one, but this one is much easier. There is no fault tolerance check for input, and it is not guaranteed that the result will not have an overflow error. It is just a boring thing for leisure:
[ Last edited by namejm on 2007-1-24 at 02:59 PM ] |
|
| Floor2 willsion | Posted 2007-01-24 23:14 |
| 高级用户 Posts 312 Credits 793 | |
|
Hehe. Just support it.
|
|
| Floor3 ccwan | Posted 2007-01-25 01:58 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Brother's code is really wonderful. I have a small question. The greatest common divisor of two numbers can be 1. Why display that there is no greatest common divisor? In mathematics, if two numbers are coprime, then their greatest common divisor is 1.
Attached is a piece of code that does not use the for statement [ Last edited by ccwan on 2007-1-25 at 06:38 AM ] |
|
| Floor4 namejm | Posted 2007-01-25 03:08 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Re ccwan 『Post #3』:
sorry, basic concepts were not remembered well, and a junior mistake was made. Your code uses the method of successive division to calculate the greatest common divisor, which is indeed a good idea, and the speed is much faster than mine. For example, when testing with 52317 and 75569, your code instantly finds the greatest common divisor (although there was an error when calculating the least common multiple), while mine takes about 10 seconds to calculate. However, my code can handle a larger range of numbers when calculating the least common multiple than yours (your code can be modified a bit). Because yours is to first multiply the two numbers and then divide by the greatest common divisor, while mine is to first divide the two numbers by the greatest common divisor and then multiply by the greatest common divisor - actually, I was a bit mechanical when calculating the least common multiple, and the code can be streamlined, that is, first divide by the greatest common divisor and then multiply by the other number. In addition, your set/a num2=%a%%%b% statement actually takes advantage of the loophole in set /a operation. I don't know if you did it on purpose or didn't notice: in set /a operation, the variables involved in the operation can be directly referenced by using the variable name without using percent pairs. According to the general understanding, the meaning of this statement is that %a% and %b% do % operation, but there is no % operator in batch processing's set/a (although there is a % operator in CMD, the preprocessing mechanism in batch processing will not recognize a single % as a modulus operator). In fact, the operation is performed according to %% operation. The possible process is: the CMD preprocessing mechanism thinks that the priority of %% is higher than the reference of percent pairs, so it expands the modulus operator first and then processes the reference of variables. At this time, it is %a modulo b%. In the set /a operation, the variable a modulo b is not found, so the percent signs before and after are discarded, and it becomes using the value of a modulo the value of b. This process is only a speculation and may not be correct. It is suggested that you change this statement to set/a num2=a%%b or set/a num2=%a%%%%b% to avoid misunderstanding. [ Last edited by namejm on 2007-1-24 at 04:00 PM ] |
|
| Floor5 ccwan | Posted 2007-01-25 03:32 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Thanks to brother jm's guidance, my consideration is: In the statement `set/a num2=%a%%%b%`, if it is changed to `a%%b` according to brother's meaning, in cmd operation, it will be regarded as `a%b`, and then the values of a and b will be searched to calculate; while my code in cmd will directly take the value of a, and then take modulo b. If a = 8, the operation formula is `set/a num2=8%b`, which reduces the search for one value and will not have an error.
This can be seen through experiments under cmd. For brother's objection, this is exactly the result of my lack of knowledge, and I can't fundamentally understand its operation mechanism. I still change it here to `set/a num2=%a%%%%b%`, which is easier to understand. [ Last edited by ccwan on 2007-1-25 at 03:50 AM ] |
|
| Floor6 20080610 | Posted 2007-01-25 04:05 |
| 初级用户 Posts 34 Credits 83 | |
| Floor7 namejm | Posted 2007-01-25 04:22 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Hehe, it turns out someone has already done it. I didn't search the old posts carefully enough, resulting in duplicate content. Guilty.
But that post appeared as a post for checking errors, and what's provided here is another kind of idea. It seems inappropriate to merge the topics for now. Let's see if there are other relatively unconventional algorithms or in-depth analyses emerging. |
|
| Floor8 pengfei | Posted 2007-01-25 05:29 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Brother namejm and Brother ccwan wrote very good code. The Euclidean algorithm is indeed the best algorithm for finding the greatest common divisor and common multiple, and it is also the most commonly used method in daily life.
When finding the common divisor, it assigns the dividend and the remainder to the divisor and dividend for the next operation respectively until the remainder is 0. [ Last edited by pengfei on 2007-1-25 at 10:32 AM ] |
|
| Floor9 lzmyst | Posted 2007-01-25 06:01 |
| 新手上路 Posts 10 Credits 19 | |
|
Temporarily unable to fully understand, first COPY it down and look at it slowly.
|
|
| Floor10 tao0610 | Posted 2007-01-25 06:07 |
| 高级用户 Posts 218 Credits 579 | |
|
....
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |