Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Zahlenrätsel (dreistellige Primzahlen) (Schleifen)

Gesucht werden alle 3stelligen Zahlen ABC, für die gilt:

  1. Die Ziffern A, B und C sind Primzahlen.
  2. Die Zahlen AB und BC sind Primzahlen.
  3. Die Zahl ABC ist eine Primzahl.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

7 Lösung(en)

def special_primes(limit: int = 999):
    non_primes = 2 * [True] + (limit-1) * [False]
    for i in range(2, limit+1):
        if not non_primes[i]:
            for j in range(2*i, limit+1, i):
                non_primes[j] = True
            if i >= 100 and all((not non_primes[idx] for idx in [*divmod(i, 10), *divmod(i, 100), i // 10 % 10])):
                yield i
print(list(special_primes()))
                

Lösung von: Name nicht veröffentlicht

Number.prototype.isPrime = function() {
  let i = 2, num = this;
  if (num == 0 || num == 1) return false;
  if (num == 2) return true;
  while (i <= Math.ceil(Math.sqrt(num))) {
    if (num % i == 0) return false;
    i++;
  }
  return true;
}

function check(str) {
  if (!parseInt(str).isPrime()) return false;
  if (!parseInt(str.slice(0,2)).isPrime()) return false;
  if (!parseInt(str.slice(1)).isPrime()) return false;
  return true;
}

let digits = [2, 3, 5, 7],
    result = [];

for (let a of digits)
  for (let b of digits)
    for (let c of digits.slice(1)) {  // 2 auslassen
      let str = '' + a + b + c;
      if (check(str)) result.push(parseInt(str));
    }

console.log(result);
                

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

// C++ 20
#include <iostream>
#include <algorithm>
#include <vector>

bool is_prime(int n) {
    if (n == 0 || n == 1) return false;
    if (n == 2) return true;
    for (auto i{ 2 }; i <= (int)ceil(sqrt(n)); ++i)
        if (n % i == 0) return false;
    return true;
}

std::tuple<int, int, int, int, int> get_numbers(int n) {
    const auto c{ n % 10 }, b{ n / 10 % 10 }, a{ n / 100 % 10 };
    return { a, b, c, a * 10 + b, b * 10 + c };
}

int main() {
    for (auto i{ 101 }; i < 999; i+=2) {
        const auto [a, b, c, ab, bc] = get_numbers(i); // C++ 20!
        const std::vector<int> n{ i, ab, bc, a, b, c };
        if (std::all_of(n.begin(), n.end(), [](auto x){ return is_prime(x); }))
            std::cout << i << "\n";
    }
}
                

Lösung von: Jens Kelm (@JKooP)

// NET 6.x | C# 10.x | VS-2022
static (int a, int b, int c, int ab, int bc) GetNumbers(int n) {
    var c = n % 10;
    var b = n / 10 % 10;
    var a = n / 100 % 10;
    return (a, b, c, a * 10 + b, b * 10 + c);
}

for (int i = 101; i < 999; i+=2) {
    var (a, b, c, ab, bc) = GetNumbers(i);
    if (new List<int> { i, ab, bc, a, b, c }.All(n => n > 1 && Enumerable.Range(1, n).Where(x => n % x == 0).SequenceEqual(new[] { 1, n })))
        Console.WriteLine(i);
}
                

Lösung von: Jens Kelm (@JKooP)

' VBA

Function IsPrime(ByVal n As Integer) As Boolean
    Dim p As Boolean
    Dim i As Integer
    p = True
    Select Case n
        Case 0 To 1: p = False
        Case 2:
        Case Else
            For i = 2 To CInt(Sqr(n))
                If n Mod i = 0 Then p = False
            Next i
    End Select
    IsPrime = p
End Function

Function GetNumbers(ByVal n As Integer) As Variant
    Dim a As Integer, b As Integer, c As Integer
    c = n Mod 10
    b = n \ 10 Mod 10
    a = n \ 100 Mod 10
    GetNumbers = Array(n, a * 10 + b, b * 10 + c, c, b, a)
End Function

Sub Main()
    Dim i As Integer
    Dim b As Boolean
    Dim n As Variant
    For i = 101 To 999 Step 2
        b = True
        For Each n In GetNumbers(i)
            If Not IsPrime(n) Then
                b = False
                Exit For
            End If
        Next
        If b Then Debug.Print i
    Next i
End Sub
                

Lösung von: Jens Kelm (@JKooP)

isprim=lambda x: True if 1!=x!=4 and sum([i for i in range(2,x//2) if x%i==0])==0  else False
for a in [i for i in range(100,1000) if isprim(i)==isprim(int(str(i)[0]))==isprim(int(str(i)[1]))==isprim(int(str(i)[2]))==isprim(int(str(i)[:2]))==isprim(int(str(i)[1:]))==True]: print(a)
                

Lösung von: rob ert (tub)

// C++ 20
// schnellere Version (Faktor 6)

#include <iostream>
#include <algorithm>
#include <array>

constexpr auto is_prime(const size_t& num_) {
    if (num_ <= 3) return num_ > 1;
    else if (num_ % 2 == 0 || num_ % 3 == 0) return false;
    else {
        for (size_t i{ 5 }; i * i < num_; i += 6)
            if (num_ % i == 0 || num_ % (i + 2) == 0) return false;
        return true;
    }
}

constexpr auto get_numbers(const size_t& num_) {
    const auto c{ num_ % 10 }, b{ num_ / 10 % 10 }, a{ num_ / 100 % 10 };
    return std::make_tuple(a, b, c, a * 10 + b, b * 10 + c );
}

int main() {
    for (auto i{ 101 }; i < 999; i += 2) {
        const auto& [a, b, c, ab, bc] { get_numbers(i) }; // C++ 20!
        const std::array<decltype(a), 5> n{ a, b, c, ab, bc };
        if (std::all_of(n.begin(), n.end(), [](const auto& x) { return is_prime(x); }))
            std::cout << i << "\n";
    }
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

373

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit:
Schwierigkeit: Leicht
Webcode: o04b-qqm2
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen