Exercises
1.3.30 Write a program GreatestCommonDivisor that finds the greatest common divisor (gcd) of two integers using Euclid’s algorithm, which is an iterative computation based on the following observation: if x is greater than y, then if y divides x,the gcd of x and y is y; otherwise, the gcd of x and y is the same as the gcd of x % y and y.
public class GreatestCommonDivisor {
public static void main(String[] args){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
if (m < n) {
int temp = m;
m = n;
n = temp;
}
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
System.out.println(n);
}
}
1.3.31 Write a program RelativelyPrime that takes an integer command-linemargument n and prints an n-by-n table such that there is an * in row i and column j if the gcd of i and j is 1 (i and j are relatively prime) and a space in that position otherwise.
public class RelativelyPrime {
public static int gcd(int m, int n){
if (m < n) {
int temp = m;
m = n;
n = temp;
}
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
return n;
}
public static void main(String[] args){
int n = Integer.parseInt(args[0]);
for (int i=1; i<=n; i++){
for (int j=1; j<=n; j++){
int s = gcd(i,j);
if(s==1){
System.out.print("*");
}
else{
System.out.print(" ");//不然会单列
}
}
System.out.println(' ');//不然会单行
}
}
}
print和println的区别:print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后。println将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。
1.3.36 Counting primes. Write a program PrimeCounter that takes an integer command-line argument n and finds the number of primes less than or equal to n. Use it to print out the number of primes less than or equal to 10 million. Note : If you are not careful, your program may not finish in a reasonable amount of time!
public class PrimeCounter {
public static boolean isPrime(int num){
for(int j = 2; j<=Math.sqrt(num);j++){
if(num%j == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
for(int j = 2; j<=n; j++){
if(isPrime(j)){
System.out.print(j+" ");
}
}
}
}
Java实现求小于n的质数的3种方法_java_脚本之家 (jb51.net)
1.3.38 Exponential function. Assume that x is a positive variable of type double. Write a code fragment that uses the Taylor series expansion to set the value of sum to
public class Exp {
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
double term = 1.0;
double sum = 0.0;
for (int n = 1; sum != sum + term; n++){
sum += term;
term *= x/n;
}
System.out.println(sum);
}
}
1.3.39 Trigonometric functions. Write two programs, Sin and Cos, that compute the sine and cosine functions using their Taylor series expansions sin x = x - (x^3)/3! + (x^5)/5! - ... and cos x = 1 - (x^2)/2! + (x^4)/4! - . . . .
public class Sin {
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
// convert x to an angle between -2 PI and 2 PI
x = x % (2 * Math.PI);
// compute the Taylor series approximation
double term = 1.0; // ith term = x^i / i!
double sum = 0.0; // sum of first i terms in taylor series
for (int i = 1; term != 0.0; i++) {
term *= (x / i);
if (i % 4 == 1) sum += term;
if (i % 4 == 3) sum -= term;
}
System.out.println(sum);
}
}