You are not logged in.
Pages: 1
I am trying to approximate the factorial function f(n)=n*(n-1)*(n-2)*...*1 using a Lagrange polynomial. The idea of the Lagrange polynomial is to know some function values and to form an approximation function.
If I understand right here are not links allowed?!?! So if you want to learn more about Lagrange Polynomial, then use Google...and same about factorial function.
I've been staring at my code for hours, looking for errors in it, but I can't find any. The Lagrange polynomial produces nonsense values. Could someone help? I would be more than grateful if someone could find a error in my code.
Known values are:
f(100)=100!
f(200)=200!
f(300)=300!
f(400)=400!
program FactorialAndLagrange;
uses math;
var f: array [1..2,0..3] of Extended;
x0,x1,x2,x3,y0,y1,y2,y3,x,Pol:Extended;
ind:Integer;
function factorial(m: Extended):Extended ; //counts m!
var i,result: Extended ;
begin
if m=0 then result:=1 else
begin
result:=1;
i:=1;
while i<=m do
begin
result:=result*i;
i:=i+1;
end;
end;
factorial:=result;
end;
begin
f[1,0]:=100;
f[1,1]:=200;
f[1,2]:=300;
f[1,3]:=400;
ind:=0;
while ind<=3 do
begin
f[2,ind]:=factorial(f[1,ind]);
writeln(f[1,ind],' ',f[2,ind]);
ind:=ind+1;
end;
x0:=f[1,0];
x1:=f[1,1];
x2:=f[1,2];
x3:=f[1,3];
y0:=f[2,0];
y1:=f[2,1];
y2:=f[2,2];
y3:=f[2,3];
x:=0;
while x<=1000 do
begin
Pol:=0;
Pol:=Pol+(((x-x1)*(x-x2)*(x-x3))/((x0-x1)*(x0-x2)*(x0-x3)))*y0;
Pol:=Pol+(((x-x0)*(x-x2)*(x-x3))/((x1-x0)*(x1-x2)*(x1-x3)))*y1;
Pol:=Pol+(((x-x0)*(x-x1)*(x-x3))/((x2-x0)*(x2-x1)*(x2-x3)))*y2;
Pol:=Pol+(((x-x0)*(x-x1)*(x-x2))/((x3-x0)*(x3-x1)*(x3-x2)))*y3;
writeln('x=',x,' ','Pol=',Pol,' = ','Factorial=',Factorial(x));
x:=x+100;
end;
end.
Ok! Thanks! Good to know reason I think I publish my pi discoveries again with no links.
Thanks! BTW. I'm little disapointed because so many of my messages was moderated? Especially the one that deal my pi discoveries.
Wild assumptions:
In one Universe there are 500 billion galaxies, each of which has 200 billion stars, of which every tenth has a planet that can be quite easily modified into a suitable place for humans to live, and the planet can hold an average of 10 billion people. At the moment, there is only one planet called Earth with life in that Universe.
Let's assume that the human population grows on average at the same rate as it has grown since 1700, when we were 640 million 35 thousand 774 about.
In this picture, the red curve describes when the number of people has multiplied by 1.0076 every year. The black curve is obtained based on actual values. That is, because that red curve "clicks" quite well with the real curve, the coefficient 1.0076 is used
Question:
In what year do man populate the Universe, after which that 1.0076 multiplication per year must be adjusted to 1.0000 multiplication? The math problem assumes, of course, that space travel is under control, and we can travel to distant stars and galaxies. This is a mathematical exercise and not a model of the real situation, as we currently have no way to travel to other stars and galaxies.
Solution: I don't know how to use logarithm calculation formulas now, but I solve the problem with a computer program: (maybe someone show me how this is done with logarithm?)
rebol[]
g: 5E+11 ;How Many galaxies?
g: (g * 2E+11) / 10 ;How many planets to populate
g: g * 1E+10 ; How many people there can be in Universe?
population: 640035774
year: 1700
until [
print [year " " population]
population: population * 1.0076
year: year + 1
population >= g
]
halt
Output:
1700 640035774
1701 644900045.8824
1702 649801286.231106
1703 654739776.006463
1704 659715798.304112
1705 664729638.371223
1706 669781583.622844
1707 674871923.658378
1708 680000950.278182
1709 685168957.500296
.
.
.
8743 9.2194892244472E+31
8744 9.289557342553E+31
8745 9.36015797835641E+31
8746 9.43129517899192E+31
8747 9.50297302235226E+31
8748 9.57519561732213E+31
8749 9.64796710401378E+31
8750 9.72129165400429E+31
8751 9.79517347057472E+31
8752 9.86961678895109E+31
8753 9.94462587654712E+31
>>
The answer is the year 8753. So pretty soon the entire universe will be populated! Right? Yes, exponential population growth is wild!
355/113=3.141592920353982300884955752212389380530973451327433628318584070796460176991150...
is a 'terrible good' approximation of pi! There is six number in divide operation and you get seven right numbers of pi. But is there even better ones? Let's examine it with Pascal program:
program approximation;
uses bigdecimalmath;
type stri = array[1..80] of char;
var a,b,approx:BigDecimal;
right,i,best,len:LongInt;
str1,str2,str3:stri;
piStr:String;
begin
piStr:='3.141592653589793238462643383279502884197169399375';
a:=1;
while a<=StrToBigDecimal('1000000000000') do
begin
str1:=BigDecimalToStr(a);
Right:=0;
b:=round(StrToBigDecimal(piStr)*a);
str2:=BigDecimalToStr(b);
approx:=divide(b,a,9999);
str3:=BigDecimalToStr(approx);
i:=1;
while(str3[i]=piStr[i]) do
begin
Right:=Right+1;
i:=i+1;
end;
len:=Length(BigDecimalToStr(a))+Length(BigDecimalToStr(b));
if (right-1)>len then
begin
writeln(str1:6,' ',str2:6,' ',len,' ',str3:10,' ',Right-1);
best:=right;
end;
a:=a+1;
end;
end.
It has run qúite a long time and still running and searching - I guess there is not same kind rational number under one trillion.
Let's try to figure out...
rebol[]
rH: 2 * 53e-12; the diameter of a hydrogen atom m
UNIage: 13.82 * 1000000000 * 365.25 * 24 * 60 * 60; the age of the universe in seconds
PlaTime: 5.39124760E-44; Planck time in seconds
c: 299792458; Speed of light m/s
age2: 60 * 60 * 24 * 365.25 * UNIage * PlaTime
dist: age2 * c
print rejoin["If the length of a year shrunk to a Planck time, the age of the universe would be " age2 " seconds, in which time light travels " dist " meters, whcich is " round/to (dist / rH) 0.0001 " times diameter of a hydrogen atom."]
halt
output:
If the length of a year shrunk to a Planck time, the age of the universe would be 7.42002306076851E-19 seconds, in which time light travels 2.2244669518044
7E-10 meters, whcich is 2.0986 times diameter of a hydrogen atom. Huh! Small?!
Greetings from Finland. I was pretty good at math in high school, but it got a little difficult in university. I studied computer science - but never graduated.
Math really is fun!
My hobby is algorithmic art, i.e. I program images and animation and nowadays also sounds.
Pages: 1