Izder456 wrote:
MateoConLechuga wrote:
Wtf is int tab[] = {};

Isn't that how do it?

Hell no.

Izder456 wrote:
edit :
I changed the int declaration from this :

Code:
int z;
int f;
int a;
int x;
int i;

to this :

Code:
uint16_t z;
uint32_t f;
uint32_t a;
uint32_t x;
uint32_t i;


but...the problem still persists

What the hell, why did you change it to these types? They are the most inefficient and large ones you could choose. I swear you are just trolling us at this point.
MateoConLechuga wrote:
Hell no.

ok then how'd I do it?
MateoConLechuga wrote:
They are the most inefficient and large ones you could choose. I swear you are just trolling us at this point.

I thought an unsigned long and unsigned int would work
would long uint8_t && uint8_t work too?

EDIT : I got dave's code working in C traslated from lua.
Here's it :

Code:
////////////////////////////////////////
// { prgmSEIVEC2 } { v1 }
// Author: izder456
// License: n/a
// Description: generate primes, but faster!
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put all your code here */
void main(void)
{
    int tab[10000];
    uint32_t p = 0;
    float q;
    uint16_t z;
    uint8_t f;
    uint16_t a;
    uint16_t x;
    uint32_t i;
    char zstr[2];
    char * ptr;
    char ques[7] = "Till? ";
    char input[] = "";
    os_ClrHomeFull();
    os_GetStringInput(ques, input, 10);
    i = strtol(input, & ptr, 0);
    for(z=2;z<=i;z++) {
        q=sqrt(z);
        f=0;
        for(x=1;x<=p;x++) {
            a=tab[x];
            if(a>q) {
                break;
            }
            if((z%a)==0) {
                f=1;
                break;
            }
        }
        if(f==0) {
            p++;
            tab[p]=z;
            sprintf(zstr, "%d", z);
            os_NewLine();
            os_PutStrFull(zstr);
        }
    }
    while(!os_GetCSC());
}

It is significantly faster that the other version, thanks dave! I did have to statically declare the tab[] array. so it has a max of 10000, but could that go any higher?

EDIT2 : There was a bug that generated numbers that are not prime so i attempted to fix that. it's now fixed

Code:
////////////////////////////////////////
// { prgmSEIVEC2 } { v1.1 }
// Author: izder456
// License: n/a
// Description: generate primes, but faster!
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put all your code here */
void print(int z); // I added this to not have something compiled twice.

void main(void)
{
    uint16_t tab[10000];
    uint16_t p = 0;
    float q;
    uint16_t z;
    uint8_t f;
    uint16_t a;
    uint16_t x;
    uint16_t i;
    char * ptr;
    char ques[7] = "Till? ";
    char input[] = "";
    os_ClrHomeFull();
    os_GetStringInput(ques, input, 10);
    i = strtol(input, & ptr, 0);
    for(z=2;z<=i;z++) {
        q=sqrt(z);
        f=0;
        for(x=1;x<=p;x++) {
            a=tab[x];
            if(a>q) {
                break;
            }
            if((z%a)==0) {
                f=1;
                break;
            }
        }
        if(f==0) {
            /*this is the hot fix code, it checks if the number is odd,
            else it's two it'll still print, which should cover all primes*/
            if(z%2!=0) {
                p++
                tab[p]=z;
                print(z);
            }
            else if(z==2) {
                p++
                tab[p]=z;
                print(z);
            }
            else;
        }
        if (os_GetCSC()) {
            os_NewLine();
            os_PutStrFull("Interrupt detected");
            break;
        }
    }
    while(!os_GetCSC());
}
void print(int z) {
    char zstr[2];
    sprintf(zstr, "%d", z);
    os_NewLine();
    os_PutStrFull(zstr);
}
Quote:

Code:
int tab[10000];


DrDnar wrote:
Oh, no no no, don't run that.

While you could statically allocate the array:
Code:
int tab[1000];
doing so would be unwise. Stack space on the CE is limited, and due to limitations in Zilog's C compiler, functions suddenly get much slower if they have more than 128 bytes of local variables.


I mean, how come you don't read.

Also, 16 bit and 32 bit integers are going to be slow and large because the processor works natively on 24 bit and 8 bit integers. So use those sizes instead.
MateoConLechuga wrote:
I mean, how come you don't read.

I did read, and I did attempt to use dynamic memory allocation, but just gave up Razz

MateoConLechuga wrote:
Also, 16 bit and 32 bit integers are going to be slow and large because the processor works natively on 24 bit and 8 bit integers. So use those sizes instead.

ok, i'll try that.

EDIT :
Here's the code with 24bit and 8bit integers. Also, is has a static array, and the input strings are statically allocated as well

Code:
////////////////////////////////////////
// { prgmSEIVEC2 } { v1.1.1 }
// Author: izder456
// License: n/a
// Description: generate primes, but faster!
////////////////////////////////////////

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

/* Standard headers */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Other available headers: stdarg.h, setjmp.h, assert.h, ctype.h, float.h, iso646.h, limits.h, errno.h */

/* uint8_t is an unsigned integer that can range from 0-255. */
/* It performs faster than just an int, so try to use it (or int8_t) when possible */

/* Put all your code here */
void print(uint24_t z);

void main(void)
{
    static uint24_t tab[10000];
    uint24_t p = 0;
    float q;
    uint24_t z;
    uint8_t f;
    uint24_t a;
    uint24_t x;
    uint24_t i;
    char * ptr;
    char ques[7] = "Till? ";
    char input[10];
    os_ClrHomeFull();
    os_GetStringInput(ques, input, sizeof(input));
    i = strtol(input, & ptr, 0);
    for(z=2;z<=i;z++) {
        q=sqrt(z);
        f=0;
        for(x=1;x<=p;x++) {
            a=tab[x];
            if(a>q) {
                break;
            }
            if((z%a)==0) {
                f=1;
                break;
            }
        }
        if(f==0) {
            if(z%2!=0) {
                p++;
                tab[p]=z;
                print(z);
            }
            else if(z==2) {
                p++;
                tab[p]=z;
                print(z);
            }
            else;
        }
        if (os_GetCSC()) {
            os_NewLine();
            os_PutStrFull("Interrupt detected");
            break;
        }
    }
    os_NewLine();
    os_PutStrFull("Done!");
    while(!os_GetCSC());
}
void print(uint24_t z) {
    char zstr[10];
    sprintf(zstr, "%u", z);
    os_NewLine();
    os_PutStrFull(zstr);
}
A tab size of 10,000 will let you calculate 10,000 primes up to 104,729. A tab size of 20,000 will let you calculate 20,000 primes up to 224,737. It takes 1 second to display 121,127 primes up to 1,600,000 on my iPad with the program above. On my TI84+CE, it takes 38 seconds to calculate 168 primes up to 1,000 .
You can optimize the declarations to decrease the size of your source file:


Code:
uint24_t p, z, a, x, i;
float q;
uint8_t f;
SomeCoolGuy wrote:
You can optimize the declarations to decrease the size of your source file:

Ok :

Code:
void print(uint24_t z);

void main(void)
{
    static uint24_t tab[10000];
    uint24_t p=0, z, a, x, i;
    float q;
    uint8_t f;
    char * ptr;
    char ques[7] = "Till? ";


    char input[10];
    os_ClrHomeFull();
    os_GetStringInput(ques, input, sizeof(input));
    i = strtol(input, & ptr, 0);
    for(z=2;z<=i;z++) {
        q=sqrt(z);
        f=0;
        for(x=1;x<=p;x++) {
            a=tab[x];
            if(a>q) {
                break;
            }
            if((z%a)==0) {
                f=1;
                break;
            }
        }
        if(f==0) {
            if(z%2!=0) {
                p++;
                tab[p]=z;
                print(z);
            }
            else if(z==2) {
                p++;
                tab[p]=z;
                print(z);
            }
            else;
        }
        if (os_GetCSC()) {
            os_NewLine();
            os_PutStrFull("Interrupt detected");
            break;
        }
    }
    os_NewLine();
    os_PutStrFull("Done!");
    while(!os_GetCSC());
}
void print(uint24_t z) {
    char zstr[10];
    sprintf(zstr, "%u", z);
    os_NewLine();
    os_PutStrFull(zstr);
}


Note : I did have to initialise p to zero to silence an error.



dave1707 wrote:
On my TI84+CE, it takes 38 seconds to calculate 168 primes up to 1,000 .

are you using the TI-BASIC programme? This c program is way faster than that.

cemetech.net wrote:
SomeCoolGuy
Advanced Newbie (Posts: 69)

nice
Izder456 On my tI84+CE, I’m running a TI Basic version of my Lua program. It’s painfully slow, so I don’t do a lot of programming on it.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement