program main !*****************************************************************************80 ! !! MAIN is the main program for SEARCH. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 22 October 2012 ! ! Author: ! ! John Burkardt ! implicit none integer ( kind = 4 ) a integer ( kind = 4 ) b integer ( kind = 4 ) c integer ( kind = 4 ) f integer ( kind = 4 ) fj integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) j real ( kind = 8 ) wtime a = 1 b = i4_huge c = 45 call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'SEARCH:' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' Search the integers from A to B' write ( *, '(a)' ) ' for a value J such that F(J) = C.' write ( *, '(a)' ) ' ' write ( *, '(a,i12)' ) ' A = ', a write ( *, '(a,i12)' ) ' B = ', b write ( *, '(a,i12)' ) ' C = ', c ! wtime = ??? call search ( a, b, c, j ) ! wtime = ??? - wtime if ( j == -1 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' No solution was found.' else write ( *, '(a)' ) ' ' write ( *, '(a,i12)' ) ' Found J = ', j write ( *, '(a,i12)' ) ' Verify F(J) = ', f ( j ) end if write ( *, '(a,g14.6)' ) ' Elapsed time is ', wtime ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'SEARCH:' write ( *, '(a)' ) ' Normal end of execution.' stop end subroutine search ( a, b, c, j ) !*****************************************************************************80 ! !! SEARCH searches integers in [A,B] for a J so that F(J) = C. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 22 October 2012 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) A, B, the search range. ! ! Input, integer ( kind = 4 ) C, the desired function value. ! ! Output, integer ( kind = 4 ) J, the computed solution, or -1 ! if no solution was found. ! implicit none integer ( kind = 4 ) a integer ( kind = 4 ) b integer ( kind = 4 ) c integer ( kind = 4 ) f integer ( kind = 4 ) fi integer ( kind = 4 ) i integer ( kind = 4 ) j j = -1 do i = a, b fi = f ( i ) if ( fi == c ) then j = i exit end if end do return end function f ( i ) !*****************************************************************************80 ! !! F is the function we are analyzing. ! ! Licensing: ! ! This code is distributed under the GNU LGPL license. ! ! Modified: ! ! 22 October 2012 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) I, the argument. ! ! Input, integer ( kind = 4 ) F, the value. ! implicit none integer ( kind = 4 ) f integer ( kind = 4 ) i integer ( kind = 4 ), parameter :: i4_huge = 2147483647 integer ( kind = 4 ) j integer ( kind = 4 ) k integer ( kind = 4 ) value value = i do j = 1, 5 k = value / 127773 value = 16807 * ( value - k * 127773 ) - k * 2836 if ( value <= 0 ) then value = value + i4_huge end if end do f = value return end