Finding the Next Power of Three with a Logical Function

In summary, the conversation is about a person struggling to understand computer languages and needing to write a function that gives the next power of three after an input number. The function involves checking if the input number is a power of three and if not, increasing the power of three until it is equal to or greater than the input number. The expected output is a logical value indicating whether the input number is a power of three or not.
  • #1
wellnis20
1
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
  • #2
Take a look at the reply to your post in Tek-Tips.
 
  • #3
Can you give some expected output?
 

Related to Finding the Next Power of Three with a Logical Function

1. What is the purpose of finding the next power of three with a logical function?

Finding the next power of three with a logical function is useful in many scientific and mathematical applications. It allows for efficient and accurate calculations involving powers of three, which are commonly used in fields such as computer science, physics, and engineering.

2. How does a logical function help in finding the next power of three?

A logical function is a mathematical operation that evaluates to either true or false based on given conditions. By using a logical function, we can determine whether a number is a power of three or not. This information can then be used to calculate the next power of three.

3. Can we use any logical function to find the next power of three?

Yes, there are multiple logical functions that can be used to find the next power of three. Some commonly used logical functions include AND, OR, and NOT. The choice of function may depend on the specific application and the conditions being evaluated.

4. Is finding the next power of three with a logical function always accurate?

Yes, using a logical function to find the next power of three will always result in an accurate calculation. This is because logical functions are based on mathematical principles and will always produce a correct output based on the given conditions.

5. Are there any limitations to using a logical function to find the next power of three?

One limitation of using a logical function is that it may not be the most efficient method for finding the next power of three in some cases. In certain situations, other mathematical operations or algorithms may be more suitable and provide faster results. Additionally, the accuracy of the calculation may depend on the precision of the numbers being used.

Similar threads

  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
10
Views
741
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
22
Views
969
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top