Warning: opendir(/var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/sh/3.0.83.2/scripts/): failed to open dir: No such file or directory in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 170
Skip to content

Gaußsche Summenformel

Nachdem ich bei einer kleinen Hilfestellung für eine Challenge festgestellt hab, dass ich den damals verwendeten Code nicht wiederfinde, hab ich mich dran gesetzt und den zur Lösung benötigten "kleinen Gauß" in C (mit GMP – The GNU Multiple Precision Arithmetic Library ) und Perl implementiert.


Der Code in C bzw. Perl sieht wie folgt aus:

C
 
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
 
int main (int argc, char **argv) {
 
        if (argc < 2) {
               
printf ("USAGE: %s numbern",argv[0]);
                return EXIT_FAILURE;
}
 
mpz_t n,m,s,t,div,solution;
mpz_init(m);
mpz_init(n);
mpz_init(s);
mpz_init(t);
mpz_init(div);
mpz_init(solution);
 
mpz_set_str(n, argv[1],10);
mpz_set_str(s, "1",10);
mpz_set_str(div, "2",10);
 
mpz_add(t, n, s);
mpz_mul(m, t, n);
mpz_fdiv_q(solution, m, div);
 
gmp_printf("%Zdn",solution);
 
mpz_clear(m);
mpz_clear(n);
mpz_clear(s);
mpz_clear(t);
mpz_clear(div);
mpz_clear(solution);
 
return EXIT_SUCCESS;
}
 
Perl
 
#!/usr/bin/perl
#Gaussche Summenformel

use strict;
use bigint;
 
my $n = $ARGV[0];
my $solution;
 
$solution = ($n*($n+1))/2;
printf ("Loesung: $solution");
 
Ein paar zusätzliche Informationen dazu gibt es im Wiki: hier ;)

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


Textile-formatting allowed
You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Form options
Imprint | Contact | Privacy Statement

Warning: opendir(/var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/sh/3.0.83.2/scripts/): failed to open dir: No such file or directory in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 170