Introduction to Programming in Java - An Interdisciplinary Approach 计算机科学导论-跨学科方法 部分练习答案1.3

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);
    }
}

java怎么求最大公约数?-java教程-PHP中文网


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 e^{x} = 1 + x + x^{2}/2! + x^{3}/3! + . . . .

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);
    }
}

Sin.java (princeton.edu)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值