C :: Aufgabe #70 :: Lösung #2

4 Lösungen Lösungen öffentlich
#70

Giuga-Zahlen berechnen

Anfänger - C von Gustl - 13.02.2015 um 12:42 Uhr
Eine natürliche Zahl n ist eine Giuga-Zahl, wenn alle ihre Primteiler p den Wert n/p - 1 teilen.

Schreibe ein Programm welches alle Giuga-Zahlen bis zu einer festen Obergrenze ausgibt.

Erläuterung zu einer Giuga-Zahl findest du hier: Wikipedia
#2
vote_ok
von Jordan (210 Punkte) - 10.03.2015 um 09:05 Uhr
Quellcode ausblenden C-Code
// Giuga Zahlen.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "math.h"

struct element
{
	int key;
	int zahl;
	struct element *next;
};

void append(struct element **liste, int value)
{
	struct element *neuesElement;									
	struct element *liste_iter = *liste;							
				
	neuesElement = (struct element*)malloc(sizeof(struct element));	
	neuesElement->zahl = value,									
	neuesElement->next = NULL;										
	

	if(liste_iter != NULL)											
	{
		while(liste_iter->next != NULL)								
		{
			liste_iter = liste_iter->next;							
																	
		}
		liste_iter->next = neuesElement;							
		neuesElement->key = liste_iter->key+1;
	}
	else
	{

		*liste = neuesElement;
		neuesElement->key = 0;
	}
}

void printliste(struct element *list)
{
	printf("das %d element hat den wert %d\n",list->key, list->zahl);										
	while (list->next != NULL)										
	{
		list = list->next;											
		printf("das %d element hat den wert %d\n",list->key, list->zahl);	
	}
	printf("\n\n");
}

element *Prim(int grenze)
{
	struct element *primListe;
	primListe = NULL;

	int teiler = 0;
	int j;
	for(int i = 2; i <= grenze; i++) 
    {
        for(j = (i-1); i%j; j--) 
        {
		}

        if(j == 1)
        {
			append(&primListe,i); 
			teiler = i;
        }
    }
	return primListe;
}

int ifPrim(int zahl, struct element *primKey)
{
	while(zahl != primKey->zahl && primKey->next != NULL)
	{
		primKey = primKey->next;
		if(primKey->zahl == zahl)
		{
			//printf("Zahl ist eine Primzahl");
			return 1;
			break;
		}
		if(primKey->zahl > zahl)
		{
			//printf("Zahl ist keine Primzahl");
			return 0;
			break;
		}

	}
	

}

element *primTeiler(int zahl, int primzahlen)
{
	struct element *primKey = Prim(primzahlen);
	struct element *primKey_start = primKey;

	struct element *primTeiler;
	primTeiler = NULL;

	int zahl_start = zahl;
	
	if(!ifPrim(zahl, primKey))
	{
		while( zahl > 1 )
		{
			while(zahl % primKey->zahl == 0)
			{
				//printf("%d * ",primKey->zahl);
				append(&primTeiler, primKey->zahl);
				zahl = zahl / primKey->zahl;				
			}
			primKey = primKey->next;

			if(ifPrim(zahl, primKey_start))
			{
				//printf("%d = %d\n\n",zahl,zahl_start);
				append(&primTeiler, zahl);
				break;
			}

			if(zahl % primKey->zahl != 0)
			{
				primKey = primKey->next;
			}
		}
	}
	else
	{
		primTeiler = NULL;
	}
	return primTeiler;

}

void Giuga(int zahl)
{
	struct element *PT =  primTeiler(zahl, 10000);
	if(PT != NULL)
	{
		while(PT->next != NULL)
		{
			//printf("%d\n",PT->zahl);
			if( ((zahl / PT->zahl) -1) % PT->zahl == 0 )
			{
				//printf("%d ist ein Teiler von %d / %d -1\n", PT->zahl, zahl, PT->zahl);
				PT = PT->next;
			}
			else
			{
				//printf("\nDie Zahl %d ist keine Giuga Zahl\n",zahl);
				break;
			}		

			if(PT->next == NULL)
			{
				if( ((zahl / PT->zahl) -1) % PT->zahl == 0 )
				{
					//printf("%d ist ein Teiler von %d / %d -1\n", PT->zahl, zahl, PT->zahl);
					//printf("\nDie Zahl %d ist eine Giuga Zahl\n",zahl);
					printf("%d\tGiuga\n",zahl);
				}		
			}
		}
	}
	else
	{
		printf("%d\tPrimzahl\n",zahl);
	}
	
}


int main()
{
	for(int i = 0; i<1000; i++)
	{
		int zahl = i;
		Giuga(zahl);
	}
		

	getchar();
	return 0;
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben