Define a function f mapping positive integers to positive integers by f(1)=1, and f(n)=f(n-1)+n if f(n-1)<=n
and f(n)=f(n-1)-n if f(n-1)>n
For n≥2,
i) Find the smallest integer n such that f(n) = 1,000,000
ii) Find the smallest integer n such that f(n) = 2,000,000
Answers have a *
Interesting fn!
rabbit-3:~ lord$ fn
n f(n)
671396 1000000*
1985808 1000000
7957423 2000000*
rabbit-3:~ lord$ more fn.f
program fn
implicit none
integer f(8000000),i
f(1)=1
do i=2,8000000
if(f(i-1).le.i)then
f(i)=f(i-1)+i
else
f(i)=f(i-1)-i
endif
if(f(i).eq.1000000.or.f(i).eq.2000000)print1,i,f(i)
1 format(i9,2x,i9)
enddo
end
Edited on August 15, 2019, 7:19 am