Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Zahlenmuster (Unterprogramme)

Programmieren Sie ein Programm, welches das folgende Zahlenmuster ausgibt: 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ... Die Zahl x kommt also x Mal vor.

Zusatzaufgabe: Schreiben Sie eine Methode muster(integer), bei welcher der Index (= Position) eingegeben werden kann und dabei die Zahl x berechnet wird. Beispiele: muster(1) -> 1, muster(6) -> 3, muster(7) -> 4. Berechnen Sie damit muster(1'000), muster(10'000) bzw. muster(1'000'000'000). Wer schafft die größten Zahlen, wer die schnellste Berechnung?

Tipp zur Zusatzaufgabe: Führen Sie im ursprünglichen Muster neben der Zahl auch den Index wie folgt mit: (1.:) 1, (2.:) 2, (3.:) 2, (4.:) 3, (5.:) 3, (6.:) 3, (7.:) 4, ...

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

4 Lösung(en)

public class Folge {
  public static void main(String[] args) {
    Folge folge = new Folge();
    for(int i = 1 ; i <= 200; i++) {
        System.out.println(i+ ": " + folge.folge(i));  }
    
    System.out.println("time       " + System.currentTimeMillis());
    System.out.println("10000      " + folge.folge(10000));
    System.out.println("time       " + System.currentTimeMillis());  }

  /** Geschlossene Lösung (s. auch iterative Lösung)
   * Liefert das i-te Element in der Folge 
   * 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 
   * Zum beispiel
   * f(1) = 1
   * f(2) = 2
   * f(3) = 2
   * f(4) = 3
   * f(198) = 20
   * f(1000000000) = 44721
   */
  int folge(long i) {
    i = i - 1; 
    // Auflösen der Gleichung:
    double k = (Math.sqrt(1 + 8*i) + 1) / 2.0;
    return (int) k; }
  
}

	// Andere Lösungen: Knuth: The art of Computer Programming Vol. 1
	//                  Kap. 1.2.4 Aufg. 41 (Seite 44 und 479)
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

// iterative Lösung (s. auch geschlossene Lösung)
int folge(int sPosition) {
  int position = 0, zahl = 0;
  while(true) {
    zahl = zahl + 1;
    position = position + zahl;
    if(position >= sPosition) {
      return zahl;  }
  }
}

                
<?xml version='1.0' encoding='utf-8' ?>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 
                      'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>

<html xmlns="http://www.w3.org/1999/xhtml" lang="de">

 <head>
  <title>Folge</title>  
  <meta http-equiv="Content-Type"        content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type"  content="text/css"        />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />

  <script type="text/javascript" src= "folge.js"></script>
 </head>

 <!--   B O D Y -->
 
 <body onload="start();"> 
  <h1>Folge</h1>
 </body>
</html>


/* FILE folge.js */

function start() {
 
  var index     = window.prompt("Geben Sie den Index ein");
  var folgeZahl = folge(index)
  window.alert("Die Folgezahl zu " + index + " lautet " + folgeZahl);
}

function folge(index) {
	// Auflösen der Gleichung nach x:
    //      (erster)index = x (x-1) / 2 + 1
	//x = (1 + Math.sqrt(8*index - 7)) / 2.0;
	// rascher: Knuth:
	x = Math.floor(Math.sqrt(2 * index) + 0.5);
    return x;
	// Andere Lösungen: Knuth: The art of Computer Programming Vol. 1
	//                  Kap. 1.2.4 Aufg. 41 (Seite 44 und 479)
}

// Begründung:
// folge(index)           index
// 1                        1
// 2                      2   3
// 3                   4    5   6
// 4                 7    8   9   10
// 5              11   12   13  14   15
// ...

//  index  x = folge(index)  x(x-1)/2+1
//                           = erster index
//                             mit x = folge(index)
//-------------------------------------------------
//  1       1                1
//  2       2                2
//  3       2                2
//  4       3                4  
//  5       3                4 
//  6       3                4
//  7       4                7
//  8       4                7 
//  9       4                7
// 10       4                7
// 11       5               11 

                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

// C++ 23 | VS-2022

#include <iostream>
#include <string>
#include <vector>
#include <print>

static const auto get_sequence_num(const uint8_t& num_) {
	return static_cast<uint64_t>((pow(10, num_) - 1) / 9 * num_);
}

static const auto get_sequence_str(const uint16_t& first_, const uint16_t& last_) {
	std::string out{};
	for (auto i{ first_ }; i <= last_; ++i) {
		for (auto k{ first_ }; k <= i; ++k)
			out += std::to_string(i) + " ";
		out += "\n";
	}
	return out;
}

static const auto get_sequence_str(const uint16_t& last_) {
	return get_sequence_str(1, last_);
}

static const auto get_number(const uint32_t& index_) {
	return floor(sqrt(2 * index_) + 0.5);
}

int main() {

	// Aufgabe 1a (Ziffern von 1 bis 9 möglich):
	for(auto i{ 1 }; i < 10; ++i)
		std::println("{}", get_sequence_num(i));
	std::println("");

	// Aufgabe 1b:
	std::println("{}", get_sequence_str(9));

	// Aufgabe 1c:
	std::println("{}", get_sequence_str(1, 25));

	// Aufgabe 2:
	std::vector<uint32_t>v{ 1, 2, 3, 1'000, 10'000, 1'000'000'000 };
	for (const auto& e : v)
		std::println("{} -> {}", e, get_number(e));

}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

muster(1) = 1

muster(2) = 2

muster(3) = 2

muster(1 000) = 45

muster(10 000) = 141

muster(1 000 000 000) = 44 721

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: k.A.
Webcode: n8f6-73nj
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen