


How to find the greatest common divisor of two integers?
本文所选的例子来自于《Advanced Bash-scripting Gudie》一书,译者 杨春敏 黄毅
1 #!/bin/bash 2 #求两个整数的最大公约数 3 4 E_BADARGS=65 5 6 #如果参数个数不为2,以参数错误退出 7 if [ $# -ne 2 ] 8 then 9 echo "Usage: `basename $0` first-number second-number"10 exit $E_BADARGS11 fi12 13 #如果参数非整数或参数值为0,以参数错误退出14 for i in $@15 do16 if [ $i=~[0-9]+ ] #"=~"后面表示要跟正则表达式,+在正则表达式中表示前面的内容至少匹配一次17 then18 if [ $i -eq 0 ]19 then20 echo "Usage: `basename $0` parameter can't be zero"21 exit $E_BADARGS22 fi23 else24 echo "Usage: `basename $0` parameter must be integer"25 exit $E_BADARGS26 fi27 done28 29 #设计一个gcd()函数,利用辗转相除法(欧几里德算法)求最大公约数30 gcd()31 {32 remainder=133 dividend=$134 divisor=$235 36 until [ $remainder -eq 0 ]37 do38 let "remainder=$dividend % $divisor"39 dividend=$divisor40 divisor=$remainder41 done42 }43 44 gcd $1 $245 46 echo "gcd of $1 and $2 is: $devidend"47 48 exit 0
在改编这个脚本的时候,我的考虑点主要有以下:
1. 所传的参数是不是要排除非整数的情况?
非整数的情况第一次我用echo $i | sed '/s/^[0-9]*$/''/g' && echo $?来排除,如果第一条命令正确执行,$?应该返回0,但是我们有更好的方法,即“=~"后面跟正则的方式
2. 参数值为0的情况是不是要排除在外?
在判断$i为整数的判断下再嵌套一个判断[ $i -eq 0 ]
3. 参数个数怎么控制?
[ $# -eq 2 ]或[ $# -ne 2 ]就可以排除空参数或参数个数不为2
4. 欧几里德算法中对于$1<$2的情况的处理?
先看$1>$2的情况
$1=65 $2=15
第一个循环:5=65 % 15
dividend=15
divisor=5
第二次循环 0=15%5
dividend=5
divisor=0
退出循环,gcd=$dividend=5
再看$1<$2的情况
$1=15 $2=65
第一次循环:15=15 % 65
dividend=65
divisor=15
第二次循环:5=65 % 15
dividend=15
divisor=5
第三次循环:0=15 % 5
dividend=5
divisor=0
退出循环,gcd=$dividend=5
可知$1<$2的情况比$1>$2的情况多了一个循环,结果是一样的
The above is the detailed content of How to find the greatest common divisor of two integers?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

The manipulation of date and time values is an important aspect of programming, and the Python language provides a useful built-in module for this called datetime. However, in some cases, you may need to convert a DateTime object to an integer value in order to perform specific operations or calculations. There are multiple ways to convert a DateTime to an integer in Python, each with its own advantages and disadvantages. In this article, we'll take a closer look at these methods and examine when each method is appropriate to use. After reading this article, you will have a complete understanding of how to efficiently convert DateTime objects to integers in Python and be able to choose the most appropriate method for your specific programming task. Method 1: Use timestamp

The regular expressions for integers are: 1. Match positive integers: ^[1-9]\d*$; 2. Match negative integers: ^-[1-9]\d*$; 3. Match positive integers and negative integers :^-?\d+$; 4. Match non-zero integers: ^(0|[1-9]\d*)$; 5. Match integers (including zero): ^-?\d+$.

Timestamp in PHP is an integer form representing time, usually the number of seconds that have passed since the first year of Unix (January 1, 1970 00:00:00 GMT). In programming, we often need to convert timestamps into other forms of integers. Here we will introduce how to convert PHP timestamps into integers, as well as specific code examples. In PHP, we can use the strtotime() function to convert the time string to a timestamp and then use date

How to use INET_ATON function in MySQL to convert IP address to integer? In network programming, the processing and storage of IP addresses are often involved. IP addresses are usually expressed in dotted-decimal form, such as 192.168.1.1. However, for some scenarios that require efficient storage and processing of IP addresses, it may be more convenient and efficient to convert the IP address to integer form. In the MySQL database, there is a built-in function called INET_ATON

Installing an SSL certificate on SharePoint is a critical step in securing your website and providing an encrypted connection. By following the correct installation steps, you can ensure the security of your website data, improve your ranking in search engines, and provide a better user experience for your visitors. Get an SSL Certificate Contact a trusted Certificate Authority (CA) to purchase an SSL certificate. Provide the required authentication and domain ownership verification information. After completing the verification process, you will receive the SSL certificate file. Prepare the Certificate File Open your SSL certificate file using a text editor. Copy the certificate contents to a new text file. Save the file as yourdomain.cer, making sure to change "yourdomain”

How to use the parseInt() method of the Integer class to convert a string into an integer. In programming, we often need to convert a string into an integer. Java provides a very convenient method, which is to use the parseInt() method of the Integer class to implement this function. This article will introduce in detail the use of the parseInt() method of the Integer class and provide some sample code for using this method. The Integer class is one of the wrapper classes that represents integers in Java.

Introduction Python integers are one of the major data types used in almost all major mathematical and logical operations. In Python, an integer is zero, a positive or negative integer, has no decimal part, and has infinite precision. They can be represented in binary, octal and hexadecimal values. In this article, we will learn how to get the sign of an integer. Methods used Use a simple mathematical comparison with zero Use the copysign() function of the math module Use the numpy.sign() function Use the abs() function to create a method Method 1: Use a mathematical comparison with zero We can use the basics of positive and negative numbers Mathematical definition to find the sign of a given integer. We use a basic if-else structure to determine the sign of a number. grammar
