Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran Help Please

Status
Not open for further replies.

wellnis20

Programmer
Joined
Mar 31, 2013
Messages
1
Location
US
Hi, so this semester I am taking an introductory class using SciTE and the language fortran. The problem is that I am extremely struggling trying to understand even the basics. I need to write a function next_power_three(n) which returns the first power of 3 after n.

Logical Function next_power_three(n)
Integer::n,p
if (n<1) then
next_power_three=.false.
return
endif
p=1
do while (.not. p>n)
if (n==p) then
next_power_three=.true.
p=p+1
return
Else
p=3*p
endif
enddo
next_power_three=.false.
endfunction

program test_next_power_three
integer:: is_power_of_three
print *,'02 ->', next_power_three(2)
print *,'03 ->', next_power_three(3)
print *,'28 ->',next_power_three(28)
endprogram

Where am I going wrong in this program? Any help is so greatly appreciated. Thanks
 
Hi wellnis20

Do you mean that you want to find the first p so that 3**P >= n (equal or grater than n) ?
Then it might be something like this that you need:

Code:
program test_next_power_three
integer next_power_three
print *,'02 ->', next_power_three(2)
print *,'03 ->', next_power_three(3)
print *,'28 ->', next_power_three(28)
end

Integer Function next_power_three(n)
Integer n,p
p = 0
do while (.true.)
   p = p+1
   if(3**p .ge. n) exit  ! or maybe .gt.
enddo
next_power_three = p
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top