| | |
| Stránka: 1 z 1
| [ Príspevkov: 11 ] | |
Autor | Správa |
---|
Registrovaný: 04.08.08 Prihlásený: 28.10.11 Príspevky: 152 Témy: 42 |
Mohol by takto vyzerat program na zistenie ci je dane cislo prvocislo ?
Ak viete jednoduchsie riesenia,prosim napiste.
Kód: program prvocisla; uses crt; var x,i,n,vysledok,delitel:integer; begin clrscr;
repeat i:=2; delitel:=0; readln(n); x:=trunc(sqrt(n)); if (n = 0) or (n = 1) then writeln('Cislo ',n,' nie je prvocislo !') else begin while (i <= x) do begin vysledok:= n mod i; if (vysledok = 0) then delitel:=delitel + 1; i:=i+1; end; if (delitel > 0) then writeln('Cislo ',n,' nie je prvocislo !') else writeln('Cislo ',n,' je prvocislo !'); end; until (n=0);
end.
|
|
Registrovaný: 30.04.08 Prihlásený: 15.05.15 Príspevky: 884 Témy: 3 |
Podla mna nemusis hladat pocet vsetkych delitelov, staci, ked najdes jedneho rozneho od 1 a uz vies, ze zadane cislo nie je prvocislo.
Kód: program prvocisla; uses crt; var x,i,n,delitel:integer; begin clrscr;
repeat i:=2; delitel:=0; readln(n); x:=trunc(sqrt(n)); if (n = 0) or (n = 1) then writeln('Cislo ',n,' nie je prvocislo !') else begin while ((i <= x) and (delitel<>1)) do begin if (n mod i=0) then delitel:=1; i:=i+1; end; if (delitel = 1) then writeln('Cislo ',n,' nie je prvocislo !') else writeln('Cislo ',n,' je prvocislo !'); end; until (n=0);
end.
V tomto pripade sa cyklus while skonci pri najdeni prveho delitela, cize nehlada ostatnych delitelov.
_________________ Empty your memory, with a free()… like a pointer! If you cast a pointer to an integer, it becomes the integer, if you cast a pointer to a struct, it becomes the struct… The pointer can crash…, and can overflow… Be a pointer my friend… |
|
Registrovaný: 16.02.08 Prihlásený: 08.12.10 Príspevky: 235 Témy: 13 | Napísal ado21: 02.11.2008 1:12 | |
|
najjednoduchsie a efektivne je casto rozdiel.
trunc/round pri praci s realnymi cislami. Ak su cisla integer, ta div/mod
preco staci kontrolovat po odmocninu?
|
|
Registrovaný: 30.04.08 Prihlásený: 15.05.15 Príspevky: 884 Témy: 3 |
No to mas tak, pokial cislo nema aspon jedneho delitela mensieho ako jeho odmocnina, tak nema ziadneho - je prvocislo.
_________________ Empty your memory, with a free()… like a pointer! If you cast a pointer to an integer, it becomes the integer, if you cast a pointer to a struct, it becomes the struct… The pointer can crash…, and can overflow… Be a pointer my friend… |
|
Registrovaný: 16.02.08 Prihlásený: 08.12.10 Príspevky: 235 Témy: 13 | Napísal ado21: 02.11.2008 16:20 | |
|
preco to mam tak?
_________________ Učet je neaktívny. |
|
Registrovaný: 04.08.08 Prihlásený: 28.10.11 Príspevky: 152 Témy: 42 | Napísal autor témy beginner: 02.11.2008 18:02 | |
|
neutromind:
no hej tvoj skript je lepsi,ak nebudes chciet vediet pocet delitelov.
|
|
Registrovaný: 30.04.08 Prihlásený: 15.05.15 Príspevky: 884 Témy: 3 |
jo jo
ado21 - ved si to skus s nejakym cislom - ak nema do svojej odmocniny nejakeho delitela, tak uz potom nema ziadneho.
_________________ Empty your memory, with a free()… like a pointer! If you cast a pointer to an integer, it becomes the integer, if you cast a pointer to a struct, it becomes the struct… The pointer can crash…, and can overflow… Be a pointer my friend… |
|
Registrovaný: 14.05.08 Prihlásený: 16.06.23 Príspevky: 99 Témy: 14 Bydlisko: Prievidzaaa... |
a čo taká 9 ? odmocnina je 3 a 9 nemá menšieho deliteľa ako 3.....
|
|
Registrovaný: 30.04.08 Prihlásený: 15.05.15 Príspevky: 884 Témy: 3 |
...mensieho alebo rovneho odmocnine To som sa pomylil, ale v tom kode to je spravne ( while ((i <= x ...)
_________________ Empty your memory, with a free()… like a pointer! If you cast a pointer to an integer, it becomes the integer, if you cast a pointer to a struct, it becomes the struct… The pointer can crash…, and can overflow… Be a pointer my friend… |
|
Registrovaný: 02.09.05 Prihlásený: 17.11.10 Príspevky: 154 Témy: 1 |
Pouzite Eratostenovo sito, od 2 po odmocninu z N nasledovnym sposobom :
Kód: #define N 102
int main() {
int i, j, a[N]; /* vsetky prvky pola oznacim 1 */ for (i = 2; i < N; i++) a[i] = 1; /* tie ktore niesu prvocisla oznacim ako 0 */ for (i = 2; i < (int)sqrt(N); i++) for (j = i; i*j < N; j++) a[i*j] = 0;
/* podmienkou if (a[i]) co je ekvivalent s if (a[i] == true) v pascali testujem ci sa dane cislo nachadza medzi prvocislami */ for (i = 2; i < N; i++) if (a[i]) printf("%4d", i);
getch(); return 0; }
_________________ Múdry nie je ten, čo veľa vie, ale ten, kto vie, čo je treba. (Thomas Alva Edison) |
|
Registrovaný: 05.01.09 Prihlásený: 04.04.16 Príspevky: 13 Témy: 5 Bydlisko: Košice |
metoda v jave:
Kód: public static boolean isPrvocislo(int number) { int j = 2; float a;
while (!(j >= number)) { a = (float) number / j; // zistenie ci cislo je cele alebo realne if (a > (int) (number / j)) { j += 1; } else { return false; } } return true; }
_________________ La morte non viene mai da sola.
"Death never comes alone" |
|
| Stránka: 1 z 1
| [ Príspevkov: 11 ] | |
| Nemôžete zakladať nové témy v tomto fóre Nemôžete odpovedať na témy v tomto fóre Nemôžete upravovať svoje príspevky v tomto fóre Nemôžete mazať svoje príspevky v tomto fóre
|
|