Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Produkt 1.000.000.000 (Schleifen)

Gesucht werden zwei natürliche Zahlen, deren Produkt eine Milliarde beträgt, wobei keine der beiden Zahlen die Ziffer Null enthalten darf. Die Komplexität der Schleife sollte besser als O(n) sein und das Ergebnis innerhalb einer Sekunde ausgegeben werden.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

6 Lösung(en)

for a in range(2,20000000):
        if '0' in str(a) or '0' in str(int(10**9/a)): continue
        if (10**9/a).is_integer():
                print(str(a)+' * '+str(int(10**9/a))+' =',str(10**9))
                break

                

Lösung von: rob ert (tub)

inp = 1_000_000_000
trailing_zeros = len(s := str(inp)) - len(s.rstrip("0"))
assert "0" not in str(a := 2**trailing_zeros)
print(f"{a} * {inp//a} = {inp}")
                

Lösung von: Name nicht veröffentlicht

Number.prototype.endsWith = function(num) {
  return this.toString().endsWith(num.toString());
}
Number.prototype.contains0 = function() {
  return this.toString().includes('0');
}

const LIM = Math.ceil(Math.sqrt(1e9));
let result = [],
    i = 2;

console.time();
while (i <= LIM) {
  if (!i.contains0()) {
    let j = 1e9 / i;
    if (Number.isInteger(j) && !j.contains0())
      result.push([i,j]);
  }
  switch (true) {
    case i.endsWith(8): i += 4; break;
    case i.endsWith(2):
    case i.endsWith(6): i += 2; break;
    default: i++;
  }
}
console.timeEnd();
console.log(result);
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET 6.0 | C# 10.x | VS 2022

var n = 1_000_000_000;
var r = n.GetPrimeFactors();
Console.WriteLine($"{n} = {r.GetResult()}");

// Lösung mittels Primfaktorzerlegung -> O(log n)
// Zeit: 10 ms
public static class Extensions {
    public static IEnumerable<int> GetPrimeFactors(this int n) {
        var i = 2;
        while (i <= n) {
            if (n % i == 0) {
                n /= i;
                yield return i;
            }
            else i++;
        }
    }

    public static string GetResult(this IEnumerable<int> lst) => string.Join(" * ", lst.GroupBy(x => x).Select(x => new { b = x.Key, e = x.Count() }).Select(x => $"{ Math.Pow(x.b, x.e) }"));
}
                

Lösung von: Jens Kelm (@JKooP)

// C++ 17
#include <iostream>
#include <vector>
#include <map>
#include <iomanip>

// Zeit: 0.03 ms

std::vector<int> get_prime_factors(int n) {
    std::vector<int>v{};
    auto i{ 2 };
    while (i <= n) {
        if (n % i == 0) {
            n /= i;
            v.push_back(i);
        }
        else ++i;
    }
    return v;
}

std::map<int, int> get_grouped_vector(const std::vector<int>& v) {
    std::map<int, int> m;
    for (const auto& i : v)
        if (m.count(i)) m[i]++;
        else m.emplace(i, 1);
    return m;
}

int main() {
    constexpr auto n{ 1'000'000'000 };
    const auto pf{ get_prime_factors(n) };
    const auto gv{ get_grouped_vector(pf) };
    auto it{ gv.begin() };
    const auto x{ pow(it->first, it->second) };
    const auto y{ pow((++it)->first, it->second) };
    std::cout << std::fixed << std::setprecision(0) << n << " -> " << x << " * "  << y << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

' VBA
' "Microsoft Scripting Runtime" einbinden

Function GetPrimeFactors(ByVal n As Long)
    Dim arr() As Integer, c As Integer
    Dim i As Long
    i = 2
    Do While i <= n
        If n Mod i = 0 Then
            n = n / i
            ReDim Preserve arr(c)
            arr(c) = i
            c = c + 1
        Else
            i = i + 1
        End If
    Loop
    GetPrimeFactors = arr
End Function

Function GetGroupedArray(arr As Variant) As Scripting.Dictionary
    Dim dic As Scripting.Dictionary
    Dim i As Variant
    Set dic = New Scripting.Dictionary
    For Each i In arr
        If dic.Exists(i) Then
            dic(i) = dic(i) + 1
        Else
            dic.Add i, 1
        End If
    Next
    Set GetGroupedArray = dic
End Function

Sub Main()
    Const NUM As Long = 1000000000
    Dim dic As Scripting.Dictionary
    Dim x As Long, y As Long
    Set dic = GetGroupedArray(GetPrimeFactors(NUM))
    x = dic.Keys(0) ^ dic.Items(0)
    y = dic.Keys(1) ^ dic.Items(1)
    Debug.Print x, y
End Sub

                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

512 und 1.953.125

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit:
Schwierigkeit: Mittel
Webcode: 3f7i-j2as
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen