Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
recupera es funzioni
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Nov 18, 2021
1 parent 274b374 commit a8a40a7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
38 changes: 38 additions & 0 deletions esercitazioni/gen_primi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <cmath>

using namespace std;

bool is_primo(const int n) {
if (n>=1 && n<=3)
return true;
if (n%2 == 0)
return false;
for (int i=3; i<=static_cast<int>(sqrt(n)); i+=2)
if (n%i == 0)
return false;
return true;
}

int prossimo_primo(const int n) {
int i;
for (i=n; !is_primo(i); i++) ;
return i;
}

int main() {
int a;
do {
cout << "Insersci a: ";
cin >> a;
} while (!(a>0));

int np = 0; // numero primo generato dalla fun
while (true) {
np = prossimo_primo(np);
if (np > a) break;
cout << np << endl;
np++;
}
return 0;
}
34 changes: 34 additions & 0 deletions esercitazioni/ric_studenti_iter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>

using namespace std;

int calcola_durata(const int tempo_domanda, const int tempo_telefonata, const int intervallo_telefonata) {
int tempo_rimanente_domanda = tempo_domanda;
int tempo_totale = 0;

do {
if (tempo_totale % intervallo_telefonata == 0)
tempo_totale += tempo_telefonata; // arriva subito una chiamata
else {
tempo_totale++;
tempo_rimanente_domanda--;
}
} while (tempo_rimanente_domanda > 0);

return tempo_totale;
}

int main() {
int t_dom, t_tel, delta_tel;
cout << "Tempo totale da dedicare alle domande: "; cin >> t_dom;
cout << "Durata di ogni telefonata: "; cin >> t_tel;
cout << "Frequenza delle telefonate. Ogni x minuti: "; cin >> delta_tel;

if (t_tel >= delta_tel) {
cout << "Il professore resterà per sempre al telefono" << endl;
}
else {
cout << "Il tempo totale sarà: " << calcola_durata(t_dom, t_tel, delta_tel) << endl;
}
return 0;
}
30 changes: 30 additions & 0 deletions esercitazioni/somma_quadrati_ottimizzato.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <cmath>

using namespace std;

int somma_quadrati(const int n, const int i) {
int j, s;
for (j = i; (s = i*i+j*j) < n; j++) ;
// cout << "\t" << s << "\t" << i << "\t" << j << endl;
return (s == n ? j : -1);
}

int main() {
int n;
int i, j;

cout << "n: "; cin >> n;

j = -1;
for (i=1; i<=int(sqrt(n/2.0)) && j==-1; i++) // i <= int(sqrt(n/2.0)) <==> i*i*2 <= n
j = somma_quadrati(n, i);
i--;

if (j != -1)
cout << i << " " << j << endl;
else
cout << "Impossibile" << endl;

return 0;
}

0 comments on commit a8a40a7

Please sign in to comment.