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

pari_precision=20
big_digit=47
field_names=$$m_ZZ,$$m_ZZ[ i ],$$m_QQ,$$m_QQ[ i ],$$m_RR,$$m_CC,,,,$$m_ZZ/$$basep$$m_ZZ
append_list= , +0*i, ,+0*i, +0.0, +0.0*i
!default append=1
!default style=simple

!if $submit_parm!=$empty
 submit_parm=
 style=expert
 noshow=yes
!endif

!if $style=simple
 append=1
 functions=no
!endif

!if $cmd=resume
 calc=resume
!endif

!if $calc=check and $cmd!=help
 compute=yes
 wims_module_log=check $check
 !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
!endif

!if $calc=resume or $calc=factor
 formula=!trim $formula
 !if $formula=$empty
  compute=
  !exit
 !endif
!endif

!if $calc=factor and $cmd!=help and $noshow!=yes
 certified=
 factor=
 phi=
 moebius=
 sigma=
 numdiv=
 compute=yes
 eq==
 !if $eq isin $formula and , notin $formula
  error_test=has_equal
  !exit
 !endif
 wims_rawmath_functions=sum,prod,deriv
 formula=!lowercase $formula
 formula=!rawmath $formula
 varlist=!varlist nofn $formula
 varcnt=!itemcnt $varlist
 !if $varcnt=2 and i isitemof $varlist
  varcnt=1
 !endif
 !for v in $varlist
  !if $v iswordof sin cos exp log ln sh ch th sinh cosh tanh tan tg\
	Argch Argsh Argth acos acosh arccos arcsin arctan arctg argch\
	argsh argth asin asinh atan atanh cot cotan ctg lg log10 sqrt\
	or ($varcnt>1 and abs isvarof $varlist)
   error_test=transcendental
   !exit
  !endif
 !next v
 n=!exec pari print($formula)
 !if $n = $empty
   error_test=?
   !exit
 !endif
 !if $n=1 or $n=-1
  error_test=unit
  !exit
 !endif
 !if $n=0
   error_test=iszero
   !exit
 !endif
 ftest=!eval $n
 !if . isin $n and NaN notin $ftest
   error_test=real
   !exit
 !endif
 wims_module_log=factor $formula
 !read myprimes

 !if $ftest=0
   error_test=iszero
   !exit
 !endif
 
 !if NaN isin $ftest
   field=1
   !if . isin $n$field_append $formula
    field=!eval $field+4
   !endif
   !if i isvarof $n$field_append $formula
    field=!eval $field+1
   !endif
   !if $field<$append
    field=$append
   !endif
 !endif
 
 !if NaN isin $ftest and $append=10
  baseprime=!trim $baseprime
  baseprime=!rawmath $baseprime
  !if $baseprime=$empty
   error_test=nobaseprime
   !exit
  !endif
  ddd=!exec pari pri=abs($baseprime);print(pri)\
	print(ispr=isprime(pri))\
	formula=$n;if(ispr,degree=poldegree(formula);coefs=Vec(formula);ddd=1;while((degree-ddd+1) && !(coefs[ddd]%pri),ddd++);print(lift(Mod(coefs[ddd],pri))))\
	if(ispr,print(lift(factormod(Pol(Mod(Vec(formula),pri)),pri))))
##	if(ispr,print(lift(factormod(formula,pri))))
  basep=!line 1 of $ddd
  isprime=!line 2 of $ddd
  factor=!line 4 of $ddd
  lead=!line 3 of $ddd
  !if matrix isin $factor
   factor=
  !endif
  content=
  basep=!trim $basep
  !if $basep=$empty
   error_test=badprime
   !exit
  !endif
  !if $isprime!=1
   error_test=notprime
   !exit
  !endif
  !if $lead=1
   lead=
  !endif
  !if $lead=0
   error_test=divisible
   !exit
  !endif
 !else
  !if NaN isin $ftest
   field_append=!item $append of $append_list
   !if $field>4
    cont=pollead
   !else
    cont=content
   !endif
   !if $varcnt>1
    append=1
    factor=!exec maxima print(Factor($formula));
    field_name=$$m_ZZ
    content=
    lead=
    maxima_factor=yes
   !else
    ddd=!exec pari formula=$n; print($cont(formula))\
	addprimes([$myprimes]);print(factor(formula$field_append))
    ddd=!mathsubst I=i in $ddd
    content=!line 1 of $ddd
    factor=!line 2 of $ddd
    lead=
   !endif
  !else
   !read Selfridge.gp
   pari_header=$Selfridge_src
   factor=!exec pari addprimes([$myprimes]);f=factor($n);print(f)\
	n=matsize(f)[1];for(k=1,n,p=f[k,1];if(p>10^6/2 && p<10^20,if(Selfridge(p)==1,print(p))))
   certified=!line 2 of $factor
   factor=!line 1 of $factor
   !if $factor=$empty and / notin $n and . notin $n
    error_test=cpu
    !exit
   !endif
  !endif number or polynomial
 !endif append=10
 
 !if $functions=yes and $maxima_factor!=yes
  f_=!translate ; to $\
$ in $factor
  factor_cnt=!linecnt $f_
  !if $ftest != NaN and $factor_cnt>1
   ddd=!exec pari print(eulerphi(formula=$n))\
   print(numdiv(formula))\
   print(sigma(formula))\
   print(moebius(formula))

   phi=!line 1 of $ddd
   numdiv=!line 2 of $ddd
   sigma=!line 3 of $ddd
   moebius=!line 4 of $ddd
   functions=
  !endif
 !endif
 error_test=
!endif

!if $calc=factor or $calc=resume
 base=!eval $n
 !read htmlfactors
 factor_cnt=$fn
 c=!item 1 of $factor
 !if $factor_cnt=1 or ($factor_cnt=2 and $c=-1)
   power=!item -1 of $factor
   !ifval $power=1
     is_prime=yes
   !endif
 !endif
!endif

field_name=!item $field of $field_names

