Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Abundante und vollkommene Zahlen (Schleifen)

 

Abundante Zahlen sind Zahlen deren Summe aller Teiler dieser Zahl außer der Zahl selbst größer ist als die Zahl. Ist die Summe gleich der Zahl, dann handelt es sich um eine vollkommene Zahl (auch perfekte oder ideale Zahlen genannt).

12 hat zum Beispiel die Teiler 1, 2, 3, 4 und 6. Deren Summe ist 16 > 12. Deswegen ist 12 eine abundante Zahl. 6 ist eine vollkommene Zahl, da 1 + 2 + 3 = 6 gilt.

Schreiben Sie ein Programm, das für eine Zahl überprüft, ob Sie abundant bzw. vollkommen ist oder nicht. Finden Sie alle vollkommenen und ungeraden abundanten Zahlen von 1 bis n.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

11 Lösung(en)

def abund(n):
    for p in range(0,n):
        i = 1
        while i<p:
            liste = []
            sum = 0
            for i in range(2,n+1):
                if p%i == 0:
                    zahl = p/i
                    liste.append(zahl)
            
            for item in liste:
                item = int(item)
                sum+=item
            
            if sum == p:
                print("!", p, " ist eine vollkommene Zahl.")
            elif sum > p:
                print(p, " ist eine abundante Zahl.")
            else:
                print(p, " ist eine defiziente Zahl.")

                               
            i+=1
                
abund(100)

                

Lösung von: Py Thon ()

package ch.programmieraufgaben.methoden;

import java.util.Scanner;

/**
 * Vollkommene Zahlen aus Programmieraufgaben.CH
 * 
 * @author Philipp Gressly (phi AT gressly DOT ch)
 */
public class VollkommeneZahlen {

    public static void main(String[] args) {
       new VollkommeneZahlen().top();
    }
    
    void top() {
      int max = einlesen("Obergrenze");
      schleife(1, max);
    }

    /**
     * Prüfe von allen Zahlen von min bis max, ob es vollkommne
     * oder abundante Zahlen sind.
     */
    void schleife(int min, int max) {
       int i = min;
       while(i <= max) {
           pruefe(i);
           i = i + 1;
       }
    }
    
    

    /**
     * Prüfe auf Vollkommenheit oder abundant-sein.
     */
    void pruefe(int testZahl) {
       int teilerSumme = teilerSumme(testZahl);
       if(teilerSumme > testZahl) {
           if(ungerade(testZahl)) {
               System.out.println(testZahl + " ist abundant.");               
           }
       }
       if(teilerSumme == testZahl) {
           System.out.println(testZahl + " ist vollkommen.");
       }
        
    }
    
    boolean ungerade(int zahl) {
       return ! gerade(zahl);
    }
    
    boolean gerade(int zahl) {
        return 0 == zahl % 2;
    }

    int teilerSumme(int zahl) {
      int teilerSumme = 0;
      int moeglicherTeiler = 1;
      while(moeglicherTeiler <= zahl / 2) {
          if(teilbar(zahl, moeglicherTeiler)) {
              teilerSumme = teilerSumme + moeglicherTeiler;
          }
          moeglicherTeiler = moeglicherTeiler + 1;
      }
      return teilerSumme;
    }
    
    /**
     * Prüfe auf Teilbarkeit
     */
    boolean teilbar(int zahl, int moeglicherTeiler) {
        return 0 == zahl % moeglicherTeiler; 
    }



    /**
     * Lese int ein.
     */
    Scanner sc = new Scanner(System.in);
    int einlesen(String wert) {
        System.out.println("Bitte " + wert + " eingeben: ");
        return sc.nextInt();
    }
    
    

} // end of class VollkommeneZahlen
                

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

object NumberUtilsTest {
	def main(args: Array[String]): Unit = {
		for(i <- 1 to 10000) {
			if(NumberUtils.isPerfect(i)) 
				println(i + " ist vollkommen.")
			else if(NumberUtils.isAbundant(i) && !NumberUtils.isEven(i))
				println(i + " ist ungerade und abundant.")
		}
	}
}

object NumberUtils {
	def isPerfect (n: Int): Boolean = n == getSumOfDivisors(n)
	def isAbundant(n: Int): Boolean = n <  getSumOfDivisors(n)
	def isEven    (n: Int): Boolean = n % 2 == 0
	
	private def getSumOfDivisors(n: Int): Int =
		(for(i <- 1 until n if n % i == 0) yield i).sum
}
                

Lösung von: Name nicht veröffentlicht

(defun echte-teiler (n)
  "Berechnet die echten Teiler einer Zahl n, also alle Teiler ohne die Zahl selbst."
  (remove n (teiler n)))

(defun teiler (n)
  "Berechnet die Teilermenge der Zahl."
	   (if (or (not (numberp n)) (floatp n))
	       nil
	       (labels
		   ((s (k list)
		      (cond ((= 1 n) (cons n nil))
			    ((= k 1) (append (list 1 n) list))
			    ((= 0 (rem n k))
			     (s (1- k) (adjoin (/ n k) (adjoin k list))))
			    (t (s (1- k) list)))))
	     (s (floor n 2) nil))))
						
(defun abundante-zahl-p (n)
  "Ist n eine abundante (Zahl deren echte Teilersumme grösser ist als sie selbst)?"
  (> (reduce #'+ (echte-teiler n)) n))

(defun ideale-zahl-p (n)
  "Ist n eine ideale (Summe aller echten Teiler von n, ist gleiche n) Zahl?"
  (= (reduce #'+ (echte-teiler n)) n))

(defun ideale-zahlen (n)
  "Berechnet alle idealen Zahlen von 1..n."
   (remove-if-not #'ideale-zahl-p (range 1 n)))

(defun ungerade-abundante-zahlen (n)
  "Berechnet alle ungeraden abundanten Zahlen von 1..n."
  (remove-if-not #'abundante-zahl-p (remove-if-not #'oddp (range 1 n))))

                

Lösung von: Stefan Meichtry (Erwachsenenbildung)

#include <stdio.h>
int main()
{
    int z, i, erg;

    for(z = 1; z < 10000; z++)
    {
        erg = 0;

        for(i = 1; i < z; i++)
        {
            if(z%i == 0)erg = erg + i;
        }

        if(erg == z)printf("%d ist Vollkommene Zahl\n", z);
        else if(erg > z && z%2==1)printf("%d ist ungerade Abundant\n", z);
    }
    return 0;
}

                

Lösung von: Christian :) (Hochschule Merseburg)

// Autor:				Andy Großhennig
// Solution for task:	Abundante und vollkommene Zahlen (Schleifen)

#include <iostream>

using namespace std;

// Function: Get the factors of the number and calculate the sum
unsigned int calculateFactor(unsigned int iNumber)
{
	unsigned int iSumofFactor = 0;

	for(int i = 1;i < iNumber;i++)
	{
		if(iNumber % i == 0)
		{
			iSumofFactor += i;
		}
	}

	return iSumofFactor;
}

// Function: Provide a Number and show their state
void checkNumber()
{
	unsigned int iNumber = 2;
	unsigned int iSumofFactor = 0;

	// Loop: Give the limit and the biggest number
	while(iNumber <= 100000)
	{
		iSumofFactor = calculateFactor(iNumber);
		if(iSumofFactor == iNumber) cout << iNumber << " ist eine vollkommene Zahl\n";
		else if(iNumber % 2 != 0 && iSumofFactor > iNumber) cout << iNumber << " ist eine ungerade abundante Zahl\n";
		iNumber++;	
	}
}

int main()
{
	checkNumber();

	cout << "\n\n";
	system("Pause");
	return 0;
}
                

Lösung von: Andy Großhennig (Bundeswehr)

 Function AbundandePruefen() 'Prüfen auf Abundand, Vollkommen oder nichts von beidem
        AbundandePruefen = False

        Dim eingabe As Integer
        Dim zähler As Integer
        Dim rest As Integer
        Dim summe As Integer

        eingabe = txtEingabe.Text

        For zähler = 1 To eingabe - 1
            rest = eingabe Mod zähler
            If rest = 0 Then
                summe = summe + zähler
            End If
        Next

        If summe > eingabe Then
            txtAusgabe.Text = eingabe & " ist eine Abundande Zahl!"
        ElseIf summe = eingabe Then
            txtAusgabe.Text = eingabe & " ist eine Vollkommene Zahl!"
        Else
            txtAusgabe.Text = eingabe & " ist normal!"
        End If

        AbundandePruefen = True
    End Function
                

Lösung von: Johannes vh ()

function checkAbundance(num) {
  var divisors = [],
      i = 1;
  if (num == 1) return 0;
  for (i; i <= Math.ceil(num / 2); i++)
    if (num % i == 0) divisors.push(i);
  i = eval(divisors.join("+"));          // i ist jetzt teilersumme
  if (i > num) return 1;                 // abundant
  if (i == num) return 2;                // vollkommen
  return 0;                              // kein befund
}

var max = prompt("Maximalwert:");
for (var n = 1; n <= max; n++) {
  switch (checkAbundance(n)) {
    case 2: document.write(n + " ist eine vollkommene Zahl<br>"); break;
    case 1: if (n % 2 != 0)
            document.write(n + " ist eine abundante ungerade Zahl<br>");
  }
}
                

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x < 10000; x++)
            {
                int z = 0;
                for (int y = 1; y < x; y++)
                {

                    if (x % y == 0)
                        z += y;
                }
                        if (z > x && x % 2 != 0)
                        {
                            Console.WriteLine("{0} ist ungerade und abundant", x);
                        } else if (z == x)
                        {
                            Console.WriteLine("{0} ist vollkommen", x);
                        } else { }
                    


                
            }
        }
    }
}

                

Lösung von: Irene Strauß (Liceo Scientifico, Bruneck)

// C++ 17

#include <iostream>
#include <map>

std::map<int, std::string> types{ {-1, "deficient"}, {0, "perfect"} , {1, "abundant"} };

std::string get_number_type(const int n) {
    auto s{ 0 };
    for(auto i{ n }; i > 1; i--)
        s += n % i == 0 ? n / i : 0;
    return types.find((s - n > 0) ? 1 : ((s - n < 0) ? -1 : 0))->second;
}

int main() {
    std::cout << "20 is " << get_number_type(20) << "\n";  // 20 is abundant
    std::cout << "10 is " << get_number_type(10) << "\n";  // 10 is deficient
    std::cout << "28 is " << get_number_type(28) << "\n";  // 28 is perfect
}
                

Lösung von: Jens Kelm (@JKooP)

' VBA

Private m_dic As Dictionary

Private Sub FillDic(ByRef dic_ As Dictionary)
    dic_.Add -1, "deficient"
    dic_.Add 0, "perfect"
    dic_.Add 1, "abundant"
End Sub

Public Function GetNumberType(ByVal num_ As Integer)
    Dim sum As Integer, i As Integer
    For i = 1 To num_ - 1
        If num_ Mod i = 0 Then sum = sum + i
    Next i
    GetNumberType = m_dic(Sgn(sum - num_))
End Function

Sub Main()
    arr = Array(20, 10, 28)
    Set m_dic = New Dictionary
    FillDic m_dic
    For i = 0 To UBound(arr)
        Debug.Print arr(i) & " -> " & GetNumberType(arr(i))
    Next i
End Sub

                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Hier: von 1 bis 10000:

6 ist vollkommen.
28 ist vollkommen.
496 ist vollkommen.
945 ist abundant.
1575 ist abundant.
2205 ist abundant.
2835 ist abundant.
3465 ist abundant.
4095 ist abundant.
4725 ist abundant.
5355 ist abundant.
5775 ist abundant.
5985 ist abundant.
6435 ist abundant.
6615 ist abundant.
6825 ist abundant.
7245 ist abundant.
7425 ist abundant.
7875 ist abundant.
8085 ist abundant.
8128 ist vollkommen.
8415 ist abundant.
8505 ist abundant.
8925 ist abundant.
9135 ist abundant.
9555 ist abundant.
9765 ist abundant.

 

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.25
Schwierigkeit: Leicht
Webcode: f9fi-7dy3
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen