# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.

!default all=1
!default all_dir=up
!default lin_dir=up
!default a=4
!default b=1
!default n=100
!default rand=30
!default plim=20
rand_max=200
rand_min=1
str_max=2000

!if $calc=check
 compute=yes
 wims_module_log=check $check
 ch=!eval $check
 !if NaN isin $check or $check=$empty
  error=bad_parm
  !exit
 !endif
 !read Selfridge.gp
 pari_header=$Selfridge_src
 test=!exec pari print(selfridge($check))
 !if $test=$empty or $test<0
  primarity=?
  !exit
 !endif
 !ifval $test=1
  primarity=yes
  l1=!charcnt $checked
  l2=!charcnt $check
  !if $l1+$l2 < 5000
   checked=$checked $check
  !endif
  !exit
 !endif
 primarity=no
 !exit
!endif

!if $calc=resume
 compute=yes
 !exit
!endif

!if $calc!=$empty and $cmd!=resume
 compute=yes
 !if $method=$empty
  error=no_method
  !exit
 !endif
 
 !if $method!=all and $method!=rand and $method!=lin
  error=bad_method
  !exit
 !endif
 
 !if $method=all
  all=!trim $all
  !if $all=$empty
   error=empty_data
   !exit
  !endif
  primes=
  wims_module_log=all $all_dir $plim from $all
  all=!rawmath $all
  all1=!eval $all
  !if NaN isin $all1
   error=bad_all
   !exit
  !endif
  !if Inf isin $all1 or $all1>40^$rand_max
   error=too_big
   !exit
  !endif
  !if $all1<-1 or ($all1<3 and $all_dir=down)
   error=too_small
   !exit
  !endif
  digits=!eval rint(log10($all1+50))
  limit=$[$str_max/($digits+3)]
  !if $all_dir=down
   primes=!exec pari n=floor($all)+1;k=1;l=0;while(k<$str_max && l<$plim,n=precprime(n-1);k=k+sizedigit(n)+2;if(n<2,break());print(n",");l++)
  !else
   primes=!exec pari n=ceil($all)-1; k=1;l=0;while(k<$str_max && l<$plim,n=nextprime(n+1);k=k+sizedigit(n)+2;print(n",");l++)
  !endif
  !goto end
 !endif
 
 !if $method=rand
  !if $rand1=prec and $primes!=$empty
   primes=!exec pari print(precprime($primes-1))
   wims_module_log=rand_prec $primes
   !goto end
  !endif
  !if $rand1=next and $primes!=$empty
   primes=!exec pari print(nextprime($primes+1))
   wims_module_log=rand_next $primes
   !goto end
  !endif
  rand=!trim $rand
  !if $rand=$empty
   error=empty_data
   !exit
  !endif
  primes=
  rand=!rawmath $rand
  randd=!eval rint($rand)
  !if NaN isin $randd
   error=bad_rand
   !exit
  !endif
  !if $randd < $rand_min
   randd=$rand_min
   rand=$rand_min
   rand_warn=min
  !endif
  !if $randd > $rand_max
   randd=$rand_max
   rand=$rand_max
   rand_warn=max
  !endif
  rn=!randint 1,9
  r1=$rn
  !if $randd>1
   !for i=2 to $randd
    j=!randint 0,9
    rn=$rn$j
   !next i
  !endif
  !if $r1>5
   primes=!exec pari print(precprime($rn))
  !else
   primes=!exec pari print(nextprime($rn))
  !endif
  wims_module_log=rand $rand: $primes
  !goto end
 !endif

 !if $method=lin
  primes=
  wims_module_log=lin $a n + $b, $lin_dir $plim from $n
  a=!rawmath $a
  b=!rawmath $b
  n=!rawmath $n
  !if $a=$empty or $b=$empty or $n=$empty
   error=empty_data
   !exit
  !endif
  a1=!eval rint($a)
  b1=!eval rint($b)
  n1=!eval rint($n)
  !if NaN isin $a1$b1$n1
   error=bad_lin
   !exit
  !endif
  !ifval $b1=0
   error=b_is_zero
   !exit
  !endif
  !ifval $a1=0
   error=a_is_zero
   !exit
  !endif
  test=$[$n1*($a1)+($b1)]
  !if Inf isin $test or $test>40^rand_max
   error=too_big
   !exit
  !endif
  !if $lin_dir=down
   primes=!exec pari n=floor($n);a=round($a);b=round($b);k=1;p=2;l=0;while(k<=$str_max && (p>1 || a<0) && l<$plim,p=n*a+b; if(p>40^$rand_max,quit); if(p>1 && isprime(p),l++;print(n" "p",");k=k+sizedigit(p)+sizedigit(n)+3);n--)
  !else
   primes=!exec pari n=ceil($n); a=round($a);b=round($b);k=1;p=2;l=0;while(k<=$str_max && (p>1 || a>0) && l<$plim,p=n*a+b; if(p>40^$rand_max,quit); if(p>1 && isprime(p),l++;print(n" "p",");k=k+sizedigit(p)+sizedigit(n)+3);n++)
  !endif
  !goto end
 !endif

 :end
 primes=!trim $primes
 c=!char -1 of $primes
 !if $c=,
  primes=!char 1 to -2 of $primes
 !endif
 !if $primes=$empty
  error=empty_$method
 !endif
!endif

