dc, 5450 bytes
dc -e"?dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-f"
Spaced out and explained:
# The idea here is to start with the input and reduce it quickly until it is
# less than what we want, then increment it until it's just right
? # Take input from stdin
d si st # Duplicate input, store in `i' and in `t'
[ # Begin macro definition (when I write in dc, "macro"=="function")
lt # Load t, our test term
2/ # Divide t by two
d st # Store a copy of this new term in `t'
d* # Duplicate and multiply (square)
li<B # Load i; if i<(t^2), execute B
] d sB x # Duplicate, store function as `B', and execute
# Loop ends when t^2 is less than i
[ # Begin macro definition
lt # Load t, our test term
1+ # Increment
d st # Store a copy of this new term in `t'
d* # Duplicate and multiply (square)
li!<A # Load i; if i>=(t^2), execute A
] d sA x # Duplicate, store function as `A', and execute
# Loop ends when t^2 == i+1
lt 1- f # Load t, decrement, and dump stack