WhiskeyCola16
Bekanntes Mitglied
wenn man logisch denk dann kommt man drauf.
ich hab die lösung für das problem nie in der schule gehört
ich hab die lösung für das problem nie in der schule gehört
Ich programmiere mit Visual Basic (Microsoft Visual Studio 8 - Express Edition) und habe schon fünf Aufgaben gelöst.Edit: Womit programmiert ihr eigentlich und wie weit seid ihr?
Wieso doppelt? Es darf doch auch Vielfache von 3 und 5 geben. In der Aufgabe steht zwar or, aber logisch gesehen ist es kein entweder-oder. Ich habe jedenfalls das richtige Ergebnis bekommen.Gut, da müsseb aber dann auch die doppelten Zahlen ausgeschlossen werden. 15 ist durch 3 und 5 teilbar.
Da stimme ich dir zu.Die Aufgaben sind zum Teil ziemlich knifflig, [...] Aber das macht irgendwie Spaß.
Wieso doppelt? Es darf doch auch Vielfache von 3 und 5 geben. In der Aufgabe steht zwar or, aber logisch gesehen ist es kein entweder-oder. Ich habe jedenfalls das richtige Ergebnis bekommen.
war gemeint(...)
wenn zahl%x==0 dann auch zahl%(zahl/x)==0;
x und zahl/x sind teiler von zahl
Also das kann ich ehrlich gesagt nicht nachvollziehen (was nicht heißt, dass ich es dir nicht glaube ). Bei mir hat ID 10 einige Minuten gebraucht.aufgabe 10 dauert bei mir 3 sekunden. tipp: IsPrime funktion optimieren
Also das kann ich ehrlich gesagt nicht nachvollziehen (was nicht heißt, dass ich es dir nicht glaube ). Bei mir hat ID 10 einige Minuten gebraucht.
[SIZE=2][COLOR=#008000]// funktion zur überprüfung ob ToBeChecked eine Primzahl ist[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][SIZE=2] IsPrime([/SIZE][SIZE=2][COLOR=#008080]Int64[/COLOR][/SIZE][SIZE=2] ToBeChecked) {[/SIZE]
[SIZE=2][COLOR=#008080] Int64[/COLOR][/SIZE][SIZE=2] check = ToBeChecked / 2;[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#008080]Int64[/COLOR][/SIZE][SIZE=2] i = 2; i < check + 1; i++) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (ToBeChecked % i == 0)[/SIZE]
[SIZE=2][COLOR=#0000ff] return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2] check = ToBeChecked / i;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#008000]// summe aller primzahlen unter 1.000.000[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] btnAufg10_Click([/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#008080]EventArgs[/COLOR][/SIZE][SIZE=2] e) {[/SIZE]
[SIZE=2][COLOR=#008080] Int64[/COLOR][/SIZE][SIZE=2] summe = 0;[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i = 2; i < 1000000; i++) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2](IsPrime(i)) summe+=i;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#008080] MessageBox[/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#800000]"Summe: "[/COLOR][/SIZE][SIZE=2] + summe);[/SIZE]
[SIZE=2]}[/SIZE]
Such mal nach: Sieb des Eratosthenes
[SIZE=2][COLOR=#008000]// primfaktorzerlegung - liefert größten primfaktor[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] btnAufg3_Click([/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#008080]EventArgs[/COLOR][/SIZE][SIZE=2] e) {[/SIZE]
[SIZE=2][COLOR=#008080] Int64[/COLOR][/SIZE][SIZE=2] zahl = 317584931803;[/SIZE]
[SIZE=2][COLOR=#008080] Int64[/COLOR][/SIZE][SIZE=2] maxPrim = 2;[/SIZE]
[SIZE=2][COLOR=#008080] Int64[/COLOR][/SIZE][SIZE=2] check = zahl;[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2] ( [/SIZE][SIZE=2][COLOR=#008080]Int64[/COLOR][/SIZE][SIZE=2] p = 2; p <= check; p++) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] ( IsPrime(p) ) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] ( check%p == 0 ) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (p > maxPrim) {[/SIZE]
[SIZE=2] maxPrim = p;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] check /= p;[/SIZE]
[SIZE=2] p--;[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#008080] MessageBox[/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#800000]"Primfaktor: "[/COLOR][/SIZE][SIZE=2] + maxPrim);[/SIZE]
[SIZE=2]}[/SIZE]
das sieb des eratosthenes ist mir bekannt. zu beginn war dies auch mein ansatz. jedoch komplizierter in der durchführung, und meiner meinung überlegung nach, müsste es auch langsamer sein. ich lasse mich aber gerne vom gegenteil überzeugen.
hänge nun selbst bei aufgabe 12. bin anscheinend schon betriebsblind, nachdem ich schon den ganzen tag aufgaben löse. hat hier jemand einen vernünftigen lösungsansatz?
[COLOR="Blue"]Private[/COLOR] [COLOR="blue"]Sub[/COLOR] Button1_Click([COLOR="blue"]ByVal[/COLOR] sender [COLOR="blue"]As[/COLOR] System.Object, [COLOR="blue"]ByVal[/COLOR] e As System.EventArgs) [COLOR="blue"]Handles[/COLOR] Button1.Click
MsgBox([COLOR="Sienna"]"Das größte Palindrom ist"[/COLOR] & [COLOR="sienna"]" "[/COLOR] & [COLOR="blue"]CStr[/COLOR](Calculate()) & [COLOR="sienna"]"."[/COLOR], MsgBoxStyle.Information)
[COLOR="Blue"]End[/COLOR] [COLOR="blue"]Sub[/COLOR]
[COLOR="blue"]Private Function[/COLOR] Calculate() [COLOR="blue"]As Integer[/COLOR]
[COLOR="blue"]Dim[/COLOR] i, j [COLOR="blue"]As Integer[/COLOR]
[COLOR="blue"]For[/COLOR] i = 999 * 999 [COLOR="blue"]To[/COLOR] 10000 [COLOR="blue"]Step[/COLOR] -1
[COLOR="blue"]If[/COLOR] [COLOR="blue"]CStr[/COLOR](i) = StrReverse([COLOR="blue"]CStr[/COLOR](i)) [COLOR="blue"]Then[/COLOR]
[COLOR="blue"]For[/COLOR] j = 999 [COLOR="blue"]To[/COLOR] 100 [COLOR="blue"]Step[/COLOR] -1
[COLOR="blue"]If[/COLOR] i [COLOR="blue"]Mod[/COLOR] j = 0 [COLOR="blue"]Then[/COLOR]
[COLOR="blue"]Return[/COLOR] i
[COLOR="blue"]End[/COLOR] [COLOR="blue"]If[/COLOR]
[COLOR="blue"]Next[/COLOR]
[COLOR="blue"]End[/COLOR] [COLOR="blue"]If[/COLOR]
[COLOR="blue"]Next[/COLOR]
[COLOR="blue"]End[/COLOR] [COLOR="blue"]Function[/COLOR]
[SIZE=2][COLOR=#0000ff]private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] btnAufg4_Click([/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#008080]EventArgs[/COLOR][/SIZE][SIZE=2] e) {[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] maxProd = 1;[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i = 100; i < 1000; i++) {[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] j = 100; j < 1000; j++) {[/SIZE]
[SIZE=2][COLOR=#0000ff] int[/COLOR][/SIZE][SIZE=2] aktProd = i * j;[/SIZE]
[SIZE=2][COLOR=#0000ff] string[/COLOR][/SIZE][SIZE=2] strProd = aktProd.ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff] char[/COLOR][/SIZE][SIZE=2][] arVerkProd = strProd.ToCharArray();[/SIZE]
[SIZE=2][COLOR=#0000ff] string[/COLOR][/SIZE][SIZE=2] strVerkProd = [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2].Empty;[/SIZE]
[SIZE=2][COLOR=#0000ff] for[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] k = arVerkProd.Length-1; k>=0; k--) {[/SIZE]
[SIZE=2] strVerkProd += arVerkProd[k];[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (strProd.Equals(strVerkProd)) {[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2].Parse(strProd) > maxProd) {[/SIZE]
[SIZE=2] maxProd = [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2].Parse(strProd);[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2][COLOR=#008080] MessageBox[/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#800000]"MaximalPalindrom: "[/COLOR][/SIZE][SIZE=2] + maxProd);[/SIZE]
[SIZE=2]}[/SIZE]
aber (999*999-1)/100=9980. also eine zahl größer als 999
Option Explicit
' Summe der Primzahlen bis 1000000 ermitteln
Public Function Aufgabe10(Optional ByVal lngBis As Long = 1000000) As Double
Dim I As Long, lngPrimzahl As Long
Dim ablnSieb() As Boolean
lngBis = 1000000
' Nach der Re-Dimensionierung haben alle Elemente
' den Wert 0 = False
ReDim ablnSieb(1 To lngBis)
' Die geradzahligen Elemente werden auf True gesetzt
For I = 4 To lngBis Step 2
ablnSieb(I) = True
Next I
' Die "1" ist keine Primzahl
ablnSieb(1) = True
lngPrimzahl = 3
Do Until (lngPrimzahl * lngPrimzahl) > lngBis
If ablnSieb(lngPrimzahl) = False Then
For I = (lngPrimzahl * lngPrimzahl) To _
lngBis Step (lngPrimzahl + lngPrimzahl)
ablnSieb(I) = True
Next I
End If
lngPrimzahl = lngPrimzahl + 2
Loop
' Die "2" ist die kleinste Primzahl
Aufgabe10 = 2
For I = 3 To lngBis Step 2
If ablnSieb(I) = False Then
Aufgabe10 = Aufgabe10 + CStr(I)
End If
Next I
End Function
[COLOR="Blue"]Private[/COLOR] [COLOR="blue"]Sub[/COLOR] Calculate_Click([COLOR="blue"]ByVal[/COLOR] sender [COLOR="blue"]As[/COLOR] System.Object, [COLOR="blue"]ByVal[/COLOR] e As System.EventArgs) [COLOR="blue"]Handles[/COLOR] Calculate.Click
MsgBox([COLOR="blue"]CStr[/COLOR](GetTriangle(divisors.Value)) & [COLOR="Sienna"]" "[/COLOR] & [COLOR="sienna"]"ist die erste Dreieckszahl mit mehr als"[/COLOR] & [COLOR="sienna"]" "[/COLOR] & [COLOR="blue"]CStr[/COLOR](divisors.Value) & [COLOR="sienna"]" "[/COLOR] & [COLOR="sienna"]"Divisoren."[/COLOR], MsgBoxStyle.Information)
[COLOR="Blue"]End[/COLOR] [COLOR="blue"]Sub[/COLOR]
[COLOR="blue"]Private[/COLOR] [COLOR="blue"]Function[/COLOR] GetTriangle([COLOR="blue"]ByVal[/COLOR] divisors [COLOR="blue"]As[/COLOR] [COLOR="blue"]Integer[/COLOR]) [COLOR="blue"]As[/COLOR] [COLOR="blue"]Integer[/COLOR]
[COLOR="blue"]Dim[/COLOR] i [COLOR="blue"]As[/COLOR] [COLOR="blue"]Integer[/COLOR] = 1 [COLOR="SeaGreen"]'increments triangle[/COLOR]
[COLOR="Blue"]Dim[/COLOR] isTriangle [COLOR="blue"]As[/COLOR] [COLOR="blue"]Boolean[/COLOR] = [COLOR="blue"]False[/COLOR]
[COLOR="blue"]Dim[/COLOR] j, k [COLOR="blue"]As[/COLOR] [COLOR="blue"]Integer[/COLOR]
[COLOR="blue"]Do[/COLOR] [COLOR="blue"]Until[/COLOR] isTriangle = [COLOR="blue"]True[/COLOR]
GetTriangle += i
[COLOR="blue"]If[/COLOR] GetTriangle > divisors [COLOR="blue"]Then[/COLOR]
k = 0 [COLOR="SeaGreen"]'counts divisors[/COLOR]
[COLOR="Blue"]For[/COLOR] j = 1 [COLOR="blue"]To[/COLOR] GetTriangle
[COLOR="blue"]If[/COLOR] GetTriangle [COLOR="blue"]Mod[/COLOR] j = 0 [COLOR="blue"]Then[/COLOR]
k += 1
[COLOR="blue"]End[/COLOR] [COLOR="blue"]If[/COLOR]
[COLOR="blue"]Next[/COLOR]
isTriangle = k > divisors
[COLOR="blue"]End[/COLOR] [COLOR="blue"]If[/COLOR]
i += 1
[COLOR="blue"]Loop[/COLOR]
[COLOR="blue"]End[/COLOR] [COLOR="blue"]Function[/COLOR]