2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

合成数列の和Advent Calendar 2018

Day 17

合成数列の和アドベントカレンダー Dart 編

Last updated at Posted at 2018-12-16

この記事は合成数列の和 Advent Calendar 2018 17日目の記事です。

ルール

  • 入力として正の整数 N を与えたら 4 から始まる 合成数 の数列の 1 番目から N 番目からの合計を出力してください
  • N は最大で 100 とします

解答

bool isPrime(int num) {
  if (num < 2) {
    return false;
  } else if (num == 2) {
    return true;
  } else if (num % 2 == 0) {
    return false;
  } else {
    for (var i = 3; i < num; i++) {
      if (num % i == 0) {
        return false;
      } 
    }
  }
  return true;
}

main(List<String> args) {
  int num = 4;
  int indata = int.parse(args[0]);
  int composites = 0;
  int sum = 0;
  while(composites < indata) {
    if(!isPrime(num)) {
      composites++;
      sum += num;
    }
    num++;
  }
  print(sum);
}

確認

$ dart sample.dart 4
27
$ dart sample.dart 10
112
$ dart sample.dart 100
7059

参考

Get Started with Server-Side Dart | Dart

macの人はbrew install dartでインストール可能

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?