Circular primes: Difference between revisions

→‎Embedded: Now use the Wren-gmp module rather than a custom C program.
m (→‎{{header|Free Pascal}}: doesn't take 2.6 s for the first 19 but 0.073s til 999999)
(→‎Embedded: Now use the Wren-gmp module rather than a custom C program.)
Line 2,704:
<br>
===Embedded===
{{libheader|GMPWren-gmp}}
A massive speed-up, of course, when GMP is plugged in for the 'probably prime' calculations. Around 11.5 minutes including the stretch goal.
<lang ecmascript>/* circular_primes_embedded.wren */
 
import "./gmp" for Mpz
import "./math" for Int
import "./fmt" for Fmt
 
var circs = []
foreign class Mpz {
construct init() {}
 
foreign setString(str, base)
 
foreign probPrime(reps)
}
 
var circs = []
var isCircular = Fn.new { |n|
var nn = n
Line 2,772 ⟶ 2,765:
var rus = []
var primes = Int.primeSieve(10000)
var repunit = Mpz.initnew()
for (p in primes[3..-1]) {
repunit.setStringsetStr("1" * p, 10)
if (repunit.probPrime(10) > 0) {
rus.add("R(%(p))")
Line 2,784 ⟶ 2,777:
System.print("\nThe following repunits are probably circular primes:")
for (i in [5003, 9887, 15073, 25031, 35317, 49081]) {
repunit.setStringsetStr("1" * i, 10)
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0)
}</lang>
<br>
and the C program to embed the above script in:
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_mpzAllocate(WrenVM* vm) {
mpz_t *pz = (mpz_t*)wrenSetSlotNewForeign(vm, 0, 0, sizeof(mpz_t));
mpz_init(*pz);
}
 
void C_setString(WrenVM* vm) {
mpz_t *pz = (mpz_t*)wrenGetSlotForeign(vm, 0);
const char *str = wrenGetSlotString(vm, 1);
int base = (int)wrenGetSlotDouble(vm, 2);
mpz_set_str(*pz, str, base);
}
 
void C_probPrime(WrenVM* vm) {
mpz_t *pz = (mpz_t*)wrenGetSlotForeign(vm, 0);
int reps = (int)wrenGetSlotDouble(vm, 1);
int ret = mpz_probab_prime_p(*pz, reps);
wrenSetSlotDouble(vm, 0, (double)ret);
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Mpz") == 0) {
methods.allocate = C_mpzAllocate;
}
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Mpz") == 0) {
if (!isStatic && strcmp(signature, "setString(_,_)") == 0) return C_setString;
if (!isStatic && strcmp(signature, "probPrime(_)") == 0) return C_probPrime;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
if( result.source) free((void*)result.source);
}
 
WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
WrenLoadModuleResult result = {0};
if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
result.onComplete = loadModuleComplete;
char fullName[strlen(name) + 6];
strcpy(fullName, name);
strcat(fullName, ".wren");
result.source = readFile(fullName);
}
return result;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
config.loadModuleFn = &loadModule;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "circular_primes_embedded.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
9,476

edits