Alain SMEJKAL on Sat, 31 Dec 2005 00:05:34 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: The problem is here:


----- Original Message ----- 
From: "Raúl Merino" <bcn_mat@hotmail.com>
To: <pari-users@list.cr.yp.to>
Sent: Friday, December 30, 2005 6:57 PM
Subject: The problem is here:


> The problem is in the last phrase, in the while.
> I want that the variable primers are a vector of GEN.
> I don't want 2^500 primes, i only want a prime x between 2^508 and 2^512.
> It's a size to big for a variable of C.

It is not easy to follow all  the given code and I guess your problem is
more simply how to use a Gen vector.
So, if it helps, here is a short sample quickly built with Gp2c that
generates such random primes using a vector.

Regards,

Alain.


Here is the Gp script to convert :
=====================
getprime()=
{
  n = 2^508 + random(2^512-2^508);
  return(nextprime(n));
}

test(n)=
{
  primers = vector(n);

  for (i=1, n,
    primers[i] = getprime();
    print("primers[" i "]:" primers[i]);
  );
}

Here is the generated C code :
====================
/*-*- compile-command: "/usr/bin/gcc -c -o
primer.gp.o -O3 -DGCC_INLINE -Wall -fno-strict-aliasing -fomit-frame-pointer
 -I/usr/local/include primer.gp.c && /usr/bin/gcc -o
primer.gp.so -shared -O3 -DGCC_INLINE -Wall -fno-strict-aliasing -fomit-fram
e-pointer -Wl,-noinhibit-exec --export-dynamic
primer.gp.o -L/usr/local/lib -lpari.dll"; -*-*/
#include <pari/pari.h>
/*
GP;install("getprime","","getprime","./primer.gp.so");
GP;install("test","vD0,G,","test","./primer.gp.so");
GP;install("init_primer","v","init_primer","./primer.gp.so");
*/
GEN getprime(void);
void test(GEN n);
void init_primer(void);
/*End of prototype*/

void
init_primer(void)   /* void */
{
  pari_sp ltop = avma;   /* ulong */
  avma = ltop;
  return;
}

GEN
getprime(void)
{
  pari_sp ltop = avma;   /* ulong */
  GEN n = polx[fetch_user_var("n")], p1;
  n = gadd(gpowgs(gen_2, 508), genrand(gsub(gpowgs(gen_2, 512),
gpowgs(gen_2, 508))));
  p1 = gnextprime(n);
  p1 = gerepileupto(ltop, p1);
  return p1;
}

void
test(GEN n)   /* void */
{
  pari_sp ltop = avma;   /* ulong */
  GEN primers = polx[fetch_user_var("primers")];
  GEN p1;   /* vec */
  {
    long l2;
    p1 = cgetg(itos(n)+1, t_VEC);
    for (l2 = 1; gcmpsg(l2, n) <= 0; ++l2)
      gel(p1, l2) = gen_0;
  }
  primers = p1;
  {
    pari_sp btop = avma, st_lim = stack_lim(btop, 1);   /* ulong */
    GEN i;
    for (i = gen_1; gcmp(i, n) <= 0; i = gaddgs(i, 1))
    {
      gel(primers, itos(i)) = getprime();
      pariputsf("primers[%Z]:%Z\n", i, gel(primers, itos(i)));
      if (low_stack(st_lim, stack_lim(btop, 1)))
        gerepileall(btop, 2, &i, &primers);
    }
  }
  avma = ltop;
  return;
}

Here what we get :
============
? install("getprime","","getprime","./primer.gp.so")
? install("test","vD0,G,","test","./primer.gp.so")
? install("init_primer","v","init_primer","./primer.gp.so")
? test(5)
primers[1]:99684198610705552240403721913399287812754901681697449520009748015
05442341246924907826896131754676423170270054
607751629932712609589958359524805563097907673
primers[2]:10373102466251827732765423494661034338667213535480178551177177644
26462199366425069267530212911925267959204539
5842872879633968305194737556725894646665803067
primers[3]:37523863628157039333562901487014298337027661465399437537175931821
36076036816653504885973654422965941922953504
171060858101065287928936440348571170348141283
primers[4]:35842629533298991606315024390919321760280893449750415689637344960
71262476156792123720757641274842102985701694
505565824493030126526669625022239411358348811
primers[5]:76349761816804581339526662264020052486645653121621615694349225619
00484263999612503274745783485111186451881530
946425275021324535646214419932209213237795817
?