Fortran-primes: A tiny Fortran library for prime numbers

It’s been in the works for quite some time, but eventually I’ve been able to pull this off.

fortran-primes

I’m happy to release fortran-primes, a tiny, modern Fortran library for handling prime numbers.

I’ve spun off some fast prime evaluation routines from my FRESCO CFD codes, which I’ve now completed with more functions that I’ve ported to modern Fortran from codes by Michal Forisek, David Deley and the Primes.jl Julia package.

The library natively integrates with the Fortran Package Manager, and I do hope it’s going to be a tiny step further toward a thriving modern Fortran ecosystem.

I welcome any feedback, benchmarks, and in general anything that could help make this library better (for example, I couldn’t find many production codes on this topic in the open source literature, so I couldn’t do benchmarks, but I’d bet it’s going to be pretty fast, since it’s mostly based on look-up tables).

14 Likes

I think I saw a statement with over 6000 continuation lines!!!

1 Like

:sunglasses:

1 Like

It looks like an interesting project. I noticed it has a "test " program/script so I thought to give it a try. However I have some problems building the code. I’m not very well-versed with FPM (or building software in general) so I’m not sure if I’m doing something wrong. Maybe I could get some advice?

First I cloned the repository:

$ git clone "https://github.com/perazz/fortran-primes.git"
$ cd fortran-primes/

Then tried running the FPM “build” and “test” commands, but they give an error for some reason.

$ fpm build
<ERROR>*cmd_build*:package error:Key fortran is not allowed in package file
STOP 1
$ fpm test
<ERROR>*cmd_run*:package error:Key fortran is not allowed in package file
STOP 1

FPM version information:

$ fpm --version
Version:     0.7.0, alpha
Program:     fpm(1)
Description: A Fortran package manager and build system
Home Page:   https://github.com/fortran-lang/fpm
License:     MIT
OS Type:     Linux

Edit: My OS is Fedora Linux 37.

Thank you! The project’s manifest was automatically generated by fpm 0.9.0 so you’ll need at least that version to build it (fortran implicit typing features appear those that are blocking your build with 0.7.0)

I have not studied the code closely, but when I skimmed over it I noticed

! Return number of trailing zeros in the bitwise representation
    elemental integer(WP) function trailing_zeros(n) result(nzero)

There is a fortran intrinsic TRAILZ() that already does this.

I noticed a couple of loops that appear to search an array for a specific value. There is also a FINDLOC() intrinsic that performs that operation.

BTW, I have oddball loops like this in my legacy codes too. These functions have been added since f77 (e.g. f2008 for TRAILZ).

1 Like

IMO rather than using the 8000 line array for a witness for 64 bit integers, you might want to use a BPSW test. It might be a little slower for prime numbers (or possibly not due to cache effects), but the code will be a ton simpler.

1 Like

@oscardssmith I agree it may be better for portability across compilers. For example, with gfortran, one can only initialize parameter arrays with size \le65536 (which can be increased to arbitrary limits, but a custom compiler flag is needed, which does not play well with integration in fpm yet). I’ve also had to borrow 128-bit integers from C to do safe exponentiation for the 64-bit integers, which is also non-standard.

@RonShepard: thanks! I didn’t know of trailz, nice function to have in Fortran. I’ve committed an update to use it.

@Fedrico - thanks for this. I found it contained useful tools. :slight_smile:

1 Like

For prime fans, I have just discovered the tiny factor command which is part of GNU coreutils:

$ factor 245646884564654891111111111111111111111111111111
245646884564654891111111111111111111111111111111: 8839 85735749949 1763581590324881 183802163423388421

DESCRIPTION
Print the prime factors of each specified integer NUMBER. If none are specified on the command line, read them from standard input.