Funktion für Quadratzahl



  • Hallo,

    ich suche eine Funktion die true zurückgibt wenn eine Zahl eine Quadratzahl ist sonst natürlich false, ich konnte bis jetzt keine solche Funktion finden brauche sie aber dringen.

    Mfg. 123kas



  • Wurde grade von einem freund auf was hingewiesen und würde gerne wissen ob das so richtig ist:

    bool Quadratzahl(int input) {
    		double input2 = System::Math::Sqrt(Convert::ToDouble(input));
    		if (input % 2 == 0) {
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    


  • 123kas schrieb:

    Wurde grade von einem freund auf was hingewiesen und würde gerne wissen ob das so richtig ist:

    bool Quadratzahl(int input) {
    		double input2 = System::Math::Sqrt(Convert::ToDouble(input)); //<-Was soll das?
    		if (input % 2 == 0) {
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    

    Also ich würde sagen ja, außer die eine Zeile (komentar). (zumindest ist der Quelltext "richtig")

    Was soll die zeile machen bzw. wofür ist die Sinnvoll? Denn die Variable input2 der du den wert zuweist wird nirgendwo gebraucht bzw. verwendet.

    Deswegen würde ich mal sagen das es auch so gehen sollte

    Boolean Quadratzahl(Int32 input) {
    		if (input % 2 == 0) 
    			return true;
    		else 
    			return false;
    	}
    


  • 123kas schrieb:

    Wurde grade von einem freund auf was hingewiesen und würde gerne wissen ob das so richtig ist:

    nein, ist es nicht. der code prüft, ob die quadratwurzel eine gerade zahl ist - das ist aber irrelevant (beispiel: 25).
    du müsstest statt dessen prüfen, ob das ergebnis der quadratwurzel eine ganze (also natürliche) zahl ist. in reinem c++ ungefähr so:

    bool quadrat = sqrt(double(zahl)) - int(sqrt(zahl)) == 0;
    


  • Also so:

    bool Quadratzahl(int input) {
    
    		double input2 = System::Math::Sqrt(Convert::ToDouble(input));
    		int input3 = Convert::ToInt32(input2);
    
    		if (input3 % 2 == 0) {
    			return true;
    		}
    		else {
    			return false;
    		}
    
    	}
    


  • Hallo,

    hab nun ein Problem, also ich hab die Funktion abgeändert da ich keine Zahl sonder eine Variable einfüge als mit Zeigern, dies hab ich gemacht da die Funktion nur Funktioniert wenn ich eine Zahl in die Klammern einfüge aber bei Variablen kommt immer true raus.

    bool* Quadratzahl(int* input) {
    
    		double *input2 = System::Math::Sqrt(Convert::ToDouble(*input));
    		int input3 = Convert::ToInt32(input2);
    
    		if (input3 % 2 == 0) {
    			return true;
    		}
    		else {
    			return false;
    		}
    
    	}
    

    Benutzt wird sie so:

    do {
    						Q2 = Q2 + 1;
    						result = Quadratzahl(&Q2);
    					 }
    
    					 while (result == false);
    

    Das Problem ist das es nur Fehler hagelt und ich keinen plan mehr habe:

    Form1.h(121) : error C2440: 'initializing' : cannot convert from 'double' to 'double *'
    Form1.h(122) : warning C4800: 'double *' : forcing value to bool 'true' or 'false' (performance warning)
    Form1.h(125) : error C2440: 'return' : cannot convert from 'bool' to 'bool *'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Form1.h(145) : error C2664: 'SquareNumber::Form1::Quadratzahl' : cannot convert parameter 1 from 'cli::interior_ptr<Type>' to 'int *'
    1> with
    1> [
    1> Type=int
    1> ]
    1> Cannot convert a managed type to an unmanaged type

    Denke für die Hilfe..



  • Die Lösung von das vieh sieht in C++/CLI ungefähr so aus:

    bool Quadratzahl(int input)
    {
    	return ((Math::Sqrt((double)input) - (int)Math::Sqrt((double)input)) == 0);
    }
    

Anmelden zum Antworten