prime_sieve 0.9.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

Prime-Sieve-Dart #

A prime number generator written in Dart, using Euler's sieve and a wheel size of 210.

The code is based as closely as possible on hacatu's Prime-Sieve.

Performance #

The performance of the prime determination is about half as fast as the C implementation. On my machine, it requires about 7.5 s (Dart) instead of 3.5 s (C) to find all primes up to 10^9.

(Printing however is way slower, instead of 3.5 s for the C version, the output takes about 80 s.)

Usage #

dart example/prime_sieve.dart [options] <max>

Determine primality for all numbers up to max. max should not exceed about 2^60 (or 2^50, when translating to JavaScript) or the size of your memory, whatever is smaller.

Options #

  • --help: Print help
  • --[no-]count: Whether to output the count of primes found. (Default: true.)
  • --[no-]print: Whether to output the primes after calculating them. (Default: true.)
  • --query: Whether to enter a query input loop for primality tests after possibly printing the values.

Change Log #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

0.9.0 - 2020-01-20 #

Added #

  • API documentation
  • Changelog

Fixed #

Changed #

  • Executable is now in example/main.dart.
  • Ran dartfmt over all the files (with the exception of _offsets in lib/prime_sieve.dart, which would really look ugly).

0.0.1 - 2020-01-19 #

Added #

Fixed #

Changed #

example/prime_sieve.dart

import 'dart:io';
import 'package:args/args.dart';
import 'package:prime_sieve/prime_sieve.dart';

bool positiveInt(String s) {
  var value = int.tryParse(s);
  return value != null && value >= 0;
}

main(List<String> args) {
  var parser = ArgParser()
    ..addFlag('help', abbr: 'h', negatable: false)
    ..addFlag('query',
        abbr: 'q', help: "Whether to enter a primality query loop")
    ..addFlag('count',
        abbr: 'c', defaultsTo: true, help: "Whether to show the count")
    ..addFlag('print',
        abbr: 'p', defaultsTo: true, help: "Whether to print the primes");
  var results = parser.parse(args);
  if (results.rest.length != 1 || !positiveInt(results.rest[0])) {
    print("""Usage: prime_sieve.dart [options] <max>
    Calculates all primes up to <max> and then performs the action(s)
    specified by the options.

""" +
        parser.usage);
    exit(1);
  }

  var primes = Sieve210(int.parse(results.rest[0]));
  if (results['count']) {
    print(primes.length);
  }
  if (results['print']) {
    for (var i = 0; i < primes.length; i++) {
      print(primes.primes[i]);
    }
  }
  if (results['query']) {
    var line;
    print("Now enter your candidate primes one per line:");
    while ((line = stdin.readLineSync()) != null) {
      if (positiveInt(line)) {
        var n = int.parse(line);
        if (primes.isPrime(n)) {
          print("prime");
        } else {
          print("not prime");
        }
      } else {
        print("invalid query");
      }
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  prime_sieve: ^0.9.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:prime_sieve/prime_sieve.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
36
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Health suggestions

Format lib/prime_sieve.dart.

Run dartfmt to format lib/prime_sieve.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
args ^1.0.0 1.6.0