recursive function game(W,N,isPrime, PrimeList) result(iswin) implicit none integer,parameter::prime=1,nonprime=0 integer,parameter::win=1,lose=0,unkown=-1 integer::N,iswin,i,this integer,allocatable::W(:),isPrime(:),PrimeList(:) if(N.le.3) then iswin = lose return else if(W(N).ne.unkown) then iswin = W(N) return else iswin = lose do i=SIZE(PrimeList),1, -1 this = PrimeList(i) if(N-this.le.2) cycle W(this) = win - game(W,N-this, isPrime, PrimeList) iswin = W(this) if(iswin .eq. win ) return end do end if end function game subroutine get_prime(isPrime, PrimeList, N) implicit none integer::i,j,N integer,parameter::prime=1,nonprime=0,unkown=-1 integer,allocatable::isPrime(:), PrimeList(:) integer,allocatable::numlist(:) isPrime=prime isPrime(1)=nonprime isPrime(2:3)=prime do i=2, N if(isPrime(i).eq.prime) then do j=2*i, N, i isPrime(j) = nonprime end do else do j=4, FLOOR(SQRT(REAL(i))) if(MOD(i,j).eq.0) then isPrime(i) = nonprime exit end if end do end if end do allocate(numlist(N)) numlist=(/(i,i=1,N)/) PrimeList=pack(numlist,isPrime(numlist).eq.prime) ! print '(2i4.3)', (i, isPrime(i), i=2,N) end subroutine get_prime program main implicit none interface recursive function game(W,N,isPrime, PrimeList) result(iswin) integer::N,iswin integer,allocatable::W(:), isPrime(:), PrimeList(:) end function game subroutine get_prime(isPrime, PrimeList, N) integer::N integer,allocatable::isPrime(:),PrimeList(:) end subroutine get_prime end interface integer::N,iswin integer,parameter::win=1,lose=0,unkown=-1 integer,allocatable::W(:),isPrime(:),PrimeList(:) read *,N allocate(W(N)) allocate(isPrime(N)) allocate(PrimeList(N)) call get_prime(isPrime, PrimeList, N) W(2:N)=unkown W(0:3)=lose iswin = game(W,N,isPrime, PrimeList) if(iswin.eq.win) then print '(a)',"Win" else print '(a)',"Lose" end if end program main