Finding the Next Power of Three with a Logical Function

  • Context: Fortran 
  • Thread starter Thread starter wellnis20
  • Start date Start date
  • Tags Tags
    Function Power
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
wellnis20
Messages
1
Reaction score
0
Hi all, I just started learning computer languages and I am having an extremely difficult time understanding it all. I need to write a function which gives the next power of three after input 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

This is what I have, but i can't see where i went wrong. Any help would be greatly appreciated.
 
Physics news on Phys.org
Take a look at the reply to your post in Tek-Tips.
 
Can you give some expected output?