help-gsl
[Top][All Lists]

## [Help-gsl] Fibonacci Erathostenes amicable perfect Pascal/Tartaglia

 From: Jorge Barros de Abreu Subject: [Help-gsl] Fibonacci Erathostenes amicable perfect Pascal/Tartaglia Date: Mon, 25 Feb 2013 04:16:58 -0300 User-agent: Mutt/1.5.21 (2010-09-15)

```Hi.

I found the below code on the internet.
May be a good idea make a similar code for gsl:

gsl_pascal or gsl_tartaglia
gsl_amicable
gsl_perfect
gsl_fibonacci

I do not find a good example code for gsl_erathostenes sieve.

This email was wroted like an suggestion of new functions
with error control and more.

By.

For Pascal triangle (Tartaglia)
#include <stdio.h>
#define D 100
int pascals(int *x, int *y, int d)
{
int i;
for (i = 1; i < d; i++)
printf("%d%c", y[i] = x[i - 1] + x[i],
i < d - 1 ? ' ' : '\n');

return D > d ? pascals(y, x, d + 1) : 0;
}

int main()
{
int x[D] = {0, 1, 0}, y[D] = {0};
return pascals(x, y, 0);
}
***********************
For amicable number
#include <stdio.h>
int main()
{
long int range, test, chk, div, sum, n1, n2;
printf("Input range to check amicable numbers: ");
scanf("%ld", &range);
test = 0;
while ( ++test < range )
{
sum = div = 0;
while ( ++div <= test/2 )
{
if ( test % div == 0 )
sum += div;
}
chk = sum;
sum = div = 0;
while ( ++div <= chk/2 )
{
if ( chk % div == 0 )
sum += div;
}
if ( sum == test )
{
if ( test == chk )
continue;
n1 = test;
if ( n1 == n2)
continue;
n2 = chk;
printf("%d\t%d\n", n1, n2);
}
}
return 0;
}
*********************
For perfect numbers:

#include<stdio.h>
int main(){
int n,i,sum;
int min,max;

printf("Enter the minimum range: ");
scanf("%d",&min);

printf("Enter the maximum range: ");
scanf("%d",&max);

printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++){
i=1;
sum = 0;

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}

if(sum==n)
printf("%d ",n);
}

return 0;
}
*********************
Fibonacci#include <stdio.h>

int fib(int n)
{
if (n < 3) return 1;
else return (fib(n-1) + fib(n-2));
}

int main(void)
{
int n, i;
printf("\n");
printf("Fibonacci\n");
printf("How many: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) printf(" %d ", fib(i));
printf("\n");
return 0;
}

--
Data Estelar 2456345,763530