第六章第一题(数学:五角数)(Math: pentagonal numbers)

第六章第一题(数学:五角数)(Math: pentagonal numbers)

  • 6.1(数学:五角数)一个五角数被定义为n(3n-1)/2,其中n = 1,2,…。所以,开始的几个数字就是1,5,12,22,…,编写具有下面所示方法头的方法,返回一个五角数:
    public static int getPentagonalNumber(int n)
    例如,getPentagonNumber(1)返回1,getPentagonNumber(2)返回5。编写一个测试程序显示前100个五角数,每行显示10个。使用%7d格式限定符来显示每个数字。
    6.1(Math: pentagonal numbers) A pentagonal number is defined as n(3n-1)/2 for n = 1 , 2 , …, and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . . . Write a method with the following header that returns a pentagonal number:
    public static int getPentagonalNumber(int n)
    For example, getPentagonalNumber(1) returns 1 and getPentagonalNumber(2) returns 5. Write a test program that uses this method to display the first 100 pentagonal numbers with 10 numbers on each line. Use the %7d format to display each number.
  • 参考代码:
package chapter06;

public class Code_01 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1;i <= 100;i++){
            System.out.printf("%7d",getPentagonalNumber(i));
            count++;
            if (count % 10 == 0)
                System.out.println();
        }

    }
    public static int getPentagonalNumber(int n){
        return n * (3 * n - 1) / 2;
    }
}

  • 结果显示:
      1      5     12     22     35     51     70     92    117    145
    176    210    247    287    330    376    425    477    532    590
    651    715    782    852    925   1001   1080   1162   1247   1335
   1426   1520   1617   1717   1820   1926   2035   2147   2262   2380
   2501   2625   2752   2882   3015   3151   3290   3432   3577   3725
   3876   4030   4187   4347   4510   4676   4845   5017   5192   5370
   5551   5735   5922   6112   6305   6501   6700   6902   7107   7315
   7526   7740   7957   8177   8400   8626   8855   9087   9322   9560
   9801  10045  10292  10542  10795  11051  11310  11572  11837  12105
  12376  12650  12927  13207  13490  13776  14065  14357  14652  14950

Process finished with exit code 0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值