UVA - 11666 Logarithms

From time immemorial different series has been an integrated part of mathematics. Series is very important for finding values of many important functions such as sin(x), ex, ln(x) etc. The well known formula for finding the value of ln(1-x) is shown below:

                                         , Here |x|<1.

However as this formula is true when x is less than 1, a modification is needed to find the formula for any integer. For any integer n the following relationship is true:

 

,

                                           Here |x|<1 and it is a real number, n is a positive integer and L is a non-negative integer.

 

But for a given integer n, L can have more than one value. Your job is to find the smallest possible value of L and for that L find the value of x.

 

Input

The input file contains around 10000 line of input. Each line contains a single integer n (0<n<231-1). Input is terminated by a line containing a zero.

 

Output

For each line of input produce one line of output. This line contains one integer followed by one floating point number. The integer number denotes the smallest possible value of L and floating-point number denotes the corresponding value of x. This floating-point number should have eight digits after the decimal point.

 

Sample Input                              Output for Sample Input

                        


Special Thanks: Arifuzzaman Arif, Sohel Hafiz, Derek Kisman


//題意:給出一個n,需要求出一個最小的L的前提下並且求出一個x (|x|<1),使得滿足題中的第二個等式。

//分析:由題中給出的第一個等式可以 得出 ln(n) = L + ln(1-x) ;  進而得到 ln(n) = ln(e^L) + ln(1-x)   ——>  ln(n) = ln((e^L)*(1-x));所以 n=(e^L)*(1-x)  ——> x=1-n/(e^L);

而由題中第二個等式分析:當x>0時,L顯然必須大於n,並且可以取得最小值爲ceil(ln(n)) 。 而 x<0的時候L可以取得最小值floor(ln(n)) 。  所以由ln(n)可以得到L的值只有兩種(先取小的計算)情況,在這兩種情況下由x=1-n/(e^L) 便可以解出x的值。若滿足|x|<1便是解。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const double e=2.718281828459045;
const double eps=1e-12;

int main()
{
	double n,ans;
	while(scanf("%lf",&n)&&n!=0.0)
	{
		int L=(int)floor(log(n)/(log(e)));
		ans=1.0-n/pow(e,1.0*L);
		if(fabs(ans)<1.0)
			printf("%d %.8lf\n",L,ans);
		else
		{
			L++,ans=1.0-n/pow(e,1.0*L);
			printf("%d %.8lf\n",L,ans);
		}
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章