3

Hi Im trying to translate this code to TI-BASIC. Im having problems with how to change for loop into while loop and also with incrementing a number in TI-BASIC.

#include <stdio.h>
int main()
{
  int n, i, flag=0;
  printf("Enter a positive integer: ");
  scanf("%d",&n);
  for(i=2;i<=n/2;++i)
  {
      if(n%i==0)
      {
          flag=1;
          break;
      }
  }
  if (flag==0)
      printf("%d is a prime number.",n);
  else
      printf("%d is not a prime number.",n);
  return 0;
}
gulftown
  • 31
  • 4
  • 1
    Your loop over `i` only needs to go to `sqrt(n)`, not `n/2`. The program is an implementation of the [Sieve of Eratosthenes](https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes). – Wai Ha Lee Apr 06 '15 at 19:15
  • Seems not having been mentioned before, therefore: TI calculators (at least in OS version 3.10) have an isPrime() function doing exactly this. So no point in implementing it by hand unless it is for educational purposes ;) – Philipp Burch Jun 14 '15 at 16:22

3 Answers3

5

You can efficiently use a While loop in this situation:

Input "NUMBER: ",A
1->B
3->I
√(A->D
If not(fPart(A/2
DelVar BWhile I<=D and B
fPart(A/I->B
I+2->I
End
If not(B
Disp "NOT
Disp "PRIME
Timtech
  • 1,224
  • 2
  • 19
  • 30
3

In TI-Basic a While loop works as you would expect and you can have conditions for it. Incrementing a number is as simple as saying

X+i->X  

Where 'i' is the incrementer.
To change a For loop into a While loop, you'll have to set up the While loop to constantly check to see if the number and increment have passed the upper bound while increasing the increment each run through.


If you wanted to mimic i++ or ++i in TI-Basic (Using a While loop), all you would have to change would be the arrangement of the code. Please note that TI-Basic For statements always operates under ++i. Example (i++):

0->X
While X<10
Disp X
X+1->X
End

This will display (With each number on a new line)

0 1 2 3 4 5 6 7 8 9

Example (++i):

0->X
While X<10
X+1->X
Disp X
End

This will display (With each number on a new line)

1 2 3 4 5 6 7 8 9 10

Let it be noted that TI-Basic For statements are much much faster than While loops when it comes to incrementing and should almost always be considered superior for the task.


Integrating Timtech's idea to skip even numbers effectively halves the required time to check the primality of the number with the addition of only a few extra lines. I expanded the idea to skip multiples of two and multiples of three.

Input "Number:",X:abs(X->X
0
If not(fPart(X/2)) or not(fPart(X/3:Return
For(B,5,sqrt(X),6)
If not(fPart(X/B)) or not(fPart((X+2)/B:Return
End
1
  • Test Number: 1003001
  • Time Required: ~4 Seconds (So much better than 15 :D)
  • Size: 65 Bytes
Zenohm
  • 548
  • 6
  • 17
  • What is the `abs(X` in the first line supposed to do? Also, I think you should run a `While` loop instead so that the loop stops as soon as it detects a factor, instead of testing all the numbers from 2 to √X (which can be quite big). – user3932000 Mar 14 '15 at 02:53
  • In practice, yes, a while loop would act as a break for the loop. In TI-Basic, the for loop is generally far superior, in terms of timing, for this purpose and any change would be to just have a goto command to break. I will revise the code so that it will break as soon as it finds a factor while retaining it's current efficiency. – Zenohm Mar 16 '15 at 22:32
  • Just for reference: using a `Goto` command to break out of any block requiring an `End` command creates a memory leak and is therefore not recommended. – user3932000 Mar 16 '15 at 22:40
  • I am aware of this, but unless this is being used inside of a loop, which would have an effect over time and eventually result in a memory error, it won't cause any noticeable effects for single primality tests. If it is being used inside of a loop, then use the program as a subroutine and have it return when it finds an factor. I am currently running timed tests to compare the for loop version to a while loop version. I will return the results. – Zenohm Mar 16 '15 at 23:38
  • Given prime number: 1003001 Using a while loop: Time: ~30 seconds Using a for loop: Time: ~16 seconds So, I revised the code so that it can be used as a function in a larger program without using Goto-Lbl and the program should still be able to work independently. (0 means Not Prime, and 1 means Prime) The answer will be stored in Ans. – Zenohm Mar 18 '15 at 22:54
2

I dont see why you would want to use a while loop as ti-basic has for loops:

0->F
Input "ENTER NUMBER:",N
For(I,2,int(N/2
If N/I=int(N/I
Then
int(N/2->I
1->F
End
End

If F
Then
Disp "NUMBER IS PRIME
Else
Disp "NUMBER IS NOT PRIME
End

N/I=int(N/I is a way to check for a number's remainder (another way of saying N%I==0 but ti basic does not have modulus). Another trick here is setting I to its maximum bound (int(N/2) as a sort of "break" like other languages would have

Lemon Drop
  • 2,113
  • 2
  • 19
  • 34