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);
}