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
Click For Summary
SUMMARY

The discussion focuses on creating a logical function in Fortran to determine the next power of three after a given integer input, n. The provided code snippet includes a function named next_power_three(n) that checks if n is a power of three and calculates the next power accordingly. The function uses a loop to multiply by three until it exceeds n, but there are logical errors in the loop condition and return statements. The expected output for inputs 2, 3, and 28 is also discussed, highlighting the need for debugging.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with logical functions and control structures
  • Basic knowledge of integer data types in programming
  • Experience with debugging and testing code
NEXT STEPS
  • Review Fortran control structures and loops
  • Learn about debugging techniques in Fortran
  • Explore mathematical functions in programming for power calculations
  • Investigate the use of recursion in logical functions
USEFUL FOR

Beginner programmers, Fortran developers, and anyone interested in learning about logical functions and debugging techniques in programming.

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.
 
Technology news on Phys.org
Take a look at the reply to your post in Tek-Tips.
 
Can you give some expected output?
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 28 ·
Replies
28
Views
6K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 29 ·
Replies
29
Views
4K