Linux tool Accumulation

Source: Internet
Author: User
Linux tool accumulation-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. calculator. Bc
Bc is a high-precision computing program originating from GNU. Apart from simple numerical computing, it can also use its unique language to write a computing program for complex computing, for example, complex series summation. Most Linux distributions contain bc, which can be found in the GnuWin32 project.
When you execute bc in the command line, you enter the bc interpreter. to exit, you can enter halt or quit. There are some minor differences between the two.
You can perform simple calculations in bc. You can enter 34*23, 3 ^ 4, and so on. "^" indicates the multiplication party. Since bc is a high-precision calculator, you can certainly enter expressions like 34 ^ 99, bc will tell you the result in a word. Wait. If you enter 3/5, you will find that the bc calculation result is 0. Is it an error?
This is a feature of bc. By default, the number of decimal places in the bc calculation result is zero. That is to say, if the calculation result is decimal, bc will remove the decimal part. Of course you can change this feature by entering:
Scale = 10
Then you let bc calculate 3/5, and you will find that the result is changed
. 6000000000
10 decimal places are retained.
Scale is a special variable in bc. By default, scale = 0, so decimal places are ignored. You can set scale to any positive integer, as long as it does not exceed 2147483647.
You can also use variables in bc. The variable name in bc starts with a lowercase letter. It can be followed by digits, lowercase letters, and underscores, but cannot contain uppercase letters. For example, execute the following statement:
A = 3
B = 2
C = 5
A * (B + c)
Bc will display the Result 21.
Next, try again:
Obase = 2
Then execute some calculations, such as 3 + 9. Bc shows 1100! What's going on?
If you are familiar with binary, you will find that 1100 of binary is equal to 12 of binary. Yes, obase is also a special variable in bc. It can set the calculation result to be displayed in that hexadecimal format. The value range of obase is 2 ~ 16. By setting different values for the obase, we can easily convert the decimal number into any hexadecimal number.
You may not be satisfied yet. Can you enter a decimal number instead? Yes. You should set the ibase variable. For example:
Ibase = 2
Obase = 2
1101 + 10011
The result displayed by bc is 100000. In this way, you can perform any hexadecimal computation by changing the values of ibase and obase.
Bc also supports a language similar to C, so it is easy to use. Write the following code in a file:
Define is_prime (n ){
Auto prime, I;
Prime = 1;
For (I = 2; I ^ 2 <= n; ++ I ){
If (n % I = 0 ){
Prime = 0;
Break;
}
}
Return prime;
} Print "[url = file: // nPlease/] \ nPlease [/url] enter a positive integer n: \ n ";
N = read ();
Print "[url = file: // n] \ n [/url]"
For (I = 2; I <= n; ++ I ){
If (is_prime (I )){
Print I;
Print "[url = file: // n/] \ n [/url]";
}
}
Quit;

If you save it as t. B, you can run the following command in the command line:
Bc-q t. B
Enter a positive integer as prompted to obtain the prime number of a string. The-q Parameter indicates that the copyright information is not displayed at the startup of bc. The first line of the Code defines a function name, which has a parameter n. The following auto prime, I defines two local variables. The next n = read () reads an integer from the keyboard. The rest are very similar to the C language and will not be explained.

Summary: as a high-precision computing program, bc is simple enough and powerful enough. The disadvantage is that it is difficult for people who have not learned programming languages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.