potenz in for-schleife
-
da ich es nicht ganz verstehe was du meinst, hier min quelltext:
#include <iostream> #include <cmath> #include <string> using namespace std; int main() { int S, n, v, loop; double pow (double base,int exponent ); double e = 2; cout << "Zu testende Primzahl : "; cin >> n; if (n % 2 != 0) { for (loop = 1; pow(e,loop) <= (n - 1); ++loop) { if (((n - 1) % 2^loop) == 0) { S = loop; } } cout << S << endl; } return 0; }
-
Was soll Zeile 9 genau?!
Mach das mal weg!
-
double pow (double base,int exponent );
Was bitte soll das sein? Ich glaube du hast padreighs Ratschlag gänzlich missverstanden.
-
ist mir grad auch aufgefallen
-
so jetzt geht anscheinend fast alles:
#include <iostream> #include <cmath> #include <string> using namespace std; int main() { int S, v, loop = 1, n; double e = 2, z ; cout << "Zu testende Primzahl : "; cin >> n; if (n % 2 != 0) { for (loop = 1; pow(e,loop) <= (n - 1); ++loop) { if (((n - 1) % pow(e,loop)) == 0) { S = loop; } } cout << S << endl; } return 0; }
nur wie kann ich jetzt mein modulo operator anwenden, da einmal ein typ integer und einmal double ist?
-
fmod oder casten.
-
ok, danke jetzt klappt alles
-
In so einem Algorithmus mit Fließkommazahlen zu hantieren, wäre mit ehrlich gesagt zu riskant. Warum nutzt Du nicht einfach den Shift-Operator?
-
ok ich hab noch nie so etwas mit einem shift operator gemacht, aber wo sollte ich ihn nutzen??
-
Gucky schrieb:
ok ich hab noch nie so etwas mit einem shift operator gemacht, aber wo sollte ich ihn nutzen??
2 hoch x ist das gleiche wie 1<<x
-
Natürlich könnte man sich auch einen Bit-Twiddling Hack zum Zählen der Null-Bits am LSB-Ende der Zahl raussuchen.