      program cg
c konjugert gradient       
      dimension volt(0:1000),cond(0:999) 
      dimension p(0:1000),r(999),ap(999)
c Tilfelding tall initialisering 
      rinv=0.5/2147483647.
      ibm=1927
      do i=1,1000
      ibm=ibm*16807
      enddo
c Genererer de 1000 konduktansene
      do i=0,999      
      ibm=ibm*16807
      cond(i)=ibm*rinv+0.5
      enddo
c Presisjonskriterium
      pres=2.e-4
c Initialiserer spenningene
      do i=0,1000      
      volt(i)=float(i)/1000
      enddo
c Initialiserer hjelpevektorer
      do i=1,999 
      p(i)=-cond(i-1)*(volt(i-1)-volt(i))-cond(i)*(volt(i+1)-volt(i))
      r(i)=p(i)
      enddo
      p(   0)=0.
      p(1000)=0.
c Iterasjonen
      do ite=1,2*1000
      rps=0.
      do i=1,999 
      rps=rps+r(i)*r(i)
      enddo
      if(sqrt(rps).le.pres) goto 200
      do i=1,999 
      ap(i)=cond(i-1)*(p(i-1)-p(i))+cond(i)*(p(i+1)-p(i))
      enddo
      am=0.
      do i=1,999 
      am=am+p(i)*ap(i)      
      enddo
      am=rps/am
      do i=1,999 
      volt(i)=volt(i)+am*p(i)
      enddo
      do i=1,999 
      r(i)=r(i)-am*ap(i)
      enddo
      rpn=0.
      do i=1,999 
      rpn=rpn+r(i)*r(i)
      enddo
      bm=rpn/rps
      do i=1,999 
      p(i)=r(i)+bm*p(i)
      enddo
      enddo
c Ferdig
200   continue
      write(*,*) ite
      end
