`

A Google's Interview Question - GLAT #20 series 3

阅读更多
The roadmap to compute all fixed points of f(x) is to start from boundaries 1, or 10^10, and keep acting f on it. If x is a boundary point, first compare x and f(x) to see whether we should generate the series using f or f inverse. Once we know which one to use, then keep doing that until we get a fixed point. Then start from the next integer again.

In order to do these, we need a few functions:
1. function f:
java 代码
  1. private long numOfOnes(long x)
  2. {
  3.     // recursion stops
  4.     if (x <= 0) return 0;
  5.     if (x == 1) return 1;
  6.     int powerk = 0;
  7.     long tenPowers = 1;
  8.     long leadingDigit = x;
  9.     while (leadingDigit >= 10)
  10.     {
  11.         leadingDigit /= 10;
  12.         powerk++;
  13.         tenPowers *= 10;
  14.     }
  15.     long reminder = x - leadingDigit * tenPowers;
  16.     if (leadingDigit == 1)
  17.     {
  18.         return powerk * tenPowers / 10 + 1 + reminder + numOfOnes(reminder);
  19.     }
  20.     else
  21.     {
  22.         return leadingDigit * powerk * tenPowers / 10 + tenPowers + numOfOnes(reminder);
  23.     }
  24. }

This is basically a direct translate of the formula (A) and (B) in Lemma 4.

2. inverse of f:
Obviously the next one is f inverse. Since f is not single valued, we have to have a better definition on the inverse. We define the inverse of f for a given y as the smallest x such that x < y <= f(x), so we could step as far as possible.
java 代码
  1. public long inverseFunc(long y)
  2. {
  3.     // recursion default.
  4.     if (y <= 0) return 0;
  5.     if (y == 1) return 1;
  6.     if (y == 2) return 10;
  7.     // check whether y is in 1's range
  8.     int m = isInOneRange(y);
  9.     if (m != -1)
  10.     {
  11.         long tmp = y - (m + 1) * power(10, m) - 1;
  12.         long inc = findRoot(tmp);
  13.         return power(10, m + 1) + inc;
  14.     }
  15.     else
  16.     {
  17.         int k = numOfDigits(y) - 1;
  18.         long tmp = y - power(10, k);
  19.         long ak = tmp / (k * power(10, k - 1));
  20.         if (ak > 9) ak = 9; // leave the rest to the recursion
  21.         long remainder = tmp - ak * k * power(10, k - 1);
  22.         long xRemainder = inverseFunc(remainder);
  23.         return ak * power(10, k) + xRemainder;
  24.     }
  25. }

The tricky part here is to figure out the power k. Here in order to know whether we should use formula (A) or (B), we need to check whether the to-be-found x has leading 1. So the helper function is
java 代码
  1. private int isInOneRange(long y)
  2. {
  3. // This is to check whether the func value is between the function
  4. // values of 10^n and 2*10^n.
  5. // this can be done faster since it's sorted, but again I am lazy
  6. int k=-1;
  7. for (int i=0; i<9; i++)
  8. {
  9. if (y <= y2[i])
  10. {
  11. k = i;
  12. break; // first smaller, break out
  13. }
  14. }
  15. if (k==-1) return -1;
  16. if (y >= y1[k]) return k;
  17. else return -1;
  18. }

where we define the following
java 代码
  1. long[] x1 = new long[9];
  2. long[] x2 = new long[9];
  3. long[] y1 = new long[9];
  4. long[] y2 = new long[9];
  5. for (int i=1; i<10; i++)
  6. {
  7. x1[i-1] = power(10, i);
  8. x2[i-1] = 2 * x1[i-1] - 1;
  9. y1[i-1] = numOfOnes(x1[i-1]);
  10. y2[i-1] = numOfOnes(x2[i-1]);
  11. }
  12. If we print out them:
  13. x range=[10, 19] y range=[2, 12]
  14. x range=[100, 199] y range=[21, 140]
  15. x range=[1000, 1999] y range=[301, 1600]
  16. x range=[10000, 19999] y range=[4001, 18000]
  17. x range=[100000, 199999] y range=[50001, 200000]
  18. x range=[1000000, 1999999] y range=[600001, 2200000]
  19. x range=[10000000, 19999999] y range=[7000001, 24000000]
  20. x range=[100000000, 199999999] y range=[80000001, 260000000]
  21. x range=[1000000000, 1999999999] y range=[900000001, 2800000000]

The other missing piece is the function foundRoot(). This is used in formula (A), which can be simplified in the form x + f(x) = y, with unknown x. We need a function to find the root x.
java 代码
  1. public long findRoot(long y)
  2. {
  3. // find a root for x + f(x) = y, where f is the number of one's function
  4. // in the above.
  5. // we need a x such that x + f(x)>=y, the largest x.
  6. // Note that x + f(x) is a mononically increasing function.
  7. long x0 = 0;
  8. long x1 = y;
  9. long x;
  10. while (x1 - x0 > 1)
  11. {
  12. x = x0 + (x1 - x0) / 2;
  13. long func = numOfOnes(x) + x;
  14. if (func == y) return x;
  15. if (func < y)
  16. {
  17. x0 = x;
  18. }
  19. else
  20. {
  21. x1 = x;
  22. }
  23. }
  24. if (x0 + numOfOnes(x0) == y) return x0;
  25. else return x1;
  26. }

Two other small functions are:
java 代码
  1. private static int numOfDigits(long x)
  2. {
  3. int i = 0;
  4. while (x > 0)
  5. {
  6. x /= 10;
  7. i++;
  8. }
  9. return i;
  10. }

which computes the number of 1's in x, and
java 代码
  1. private static long power(long base, int power)
  2. {
  3. long ret = 1;
  4. for (int i=1; i<=power; i++) ret *= base;
  5. return ret;
  6. }

which computes the power for the performance reason. With all of these in places, the only thing left is a "conductor" method:
java 代码
  1. public void countDigitOne()
  2. {
  3. long begin = System.currentTimeMillis();
  4. long loop = 10000000000L;
  5. int iterationTimes = 1;
  6. while (loop > 0 && iterationTimes < ITERATION_LIMIT)
  7. {
  8. long func = numOfOnes(loop);
  9. if (func == loop)
  10. {
  11. System.out.println("-----> found a fixed point: x=" + loop);
  12. // now skip this one and keep going for the next one.
  13. loop--;
  14. }
  15. else
  16. {
  17. if (func < loop) // we force this to go to the left.
  18. {
  19. loop = func;
  20. }
  21. else // func > loop
  22. {
  23. func = inverseFunc(loop);
  24. if (func < loop && numOfOnes(func) >= loop)
  25. {
  26. loop = func;
  27. }
  28. else
  29. {
  30. loop --; // if we can't find one, just decrement it.
  31. }
  32. }
  33. }
  34. iterationTimes++;
  35. }
  36. System.out.println("It takes " + (System.currentTimeMillis() - begin) + " milli");
  37. System.out.println("It takes " + iterationTimes + " iterations");
  38. }

The result is:

-----> found a fixed point: x=1111111110
-----> found a fixed point: x=535200001
-----> found a fixed point: x=535200000
-----> found a fixed point: x=535199990
-----> found a fixed point: x=535199989
-----> found a fixed point: x=535199988
-----> found a fixed point: x=535199987
-----> found a fixed point: x=535199986
-----> found a fixed point: x=535199985
-----> found a fixed point: x=535199984
-----> found a fixed point: x=535199983
-----> found a fixed point: x=535199982
-----> found a fixed point: x=535199981
-----> found a fixed point: x=535000001
-----> found a fixed point: x=535000000
-----> found a fixed point: x=513199998
-----> found a fixed point: x=502600001
-----> found a fixed point: x=502600000
-----> found a fixed point: x=501599990
-----> found a fixed point: x=501599989
-----> found a fixed point: x=501599988
-----> found a fixed point: x=501599987
-----> found a fixed point: x=501599986
-----> found a fixed point: x=501599985
-----> found a fixed point: x=501599984
-----> found a fixed point: x=501599983
-----> found a fixed point: x=501599982
-----> found a fixed point: x=501599981
-----> found a fixed point: x=500200001
-----> found a fixed point: x=500200000
-----> found a fixed point: x=500199990
-----> found a fixed point: x=500199989
-----> found a fixed point: x=500199988
-----> found a fixed point: x=500199987
-----> found a fixed point: x=500199986
-----> found a fixed point: x=500199985
-----> found a fixed point: x=500199984
-----> found a fixed point: x=500199983
-----> found a fixed point: x=500199982
-----> found a fixed point: x=500199981
-----> found a fixed point: x=500000001
-----> found a fixed point: x=500000000
-----> found a fixed point: x=117463825
-----> found a fixed point: x=35200001
-----> found a fixed point: x=35200000
-----> found a fixed point: x=35199990
-----> found a fixed point: x=35199989
-----> found a fixed point: x=35199988
-----> found a fixed point: x=35199987
-----> found a fixed point: x=35199986
-----> found a fixed point: x=35199985
-----> found a fixed point: x=35199984
-----> found a fixed point: x=35199983
-----> found a fixed point: x=35199982
-----> found a fixed point: x=35199981
-----> found a fixed point: x=35000001
-----> found a fixed point: x=35000000
-----> found a fixed point: x=13199998
-----> found a fixed point: x=2600001
-----> found a fixed point: x=2600000
-----> found a fixed point: x=1599990
-----> found a fixed point: x=1599989
-----> found a fixed point: x=1599988
-----> found a fixed point: x=1599987
-----> found a fixed point: x=1599986
-----> found a fixed point: x=1599985
-----> found a fixed point: x=1599984
-----> found a fixed point: x=1599983
-----> found a fixed point: x=1599982
-----> found a fixed point: x=1599981
-----> found a fixed point: x=200001
-----> found a fixed point: x=200000
-----> found a fixed point: x=199990
-----> found a fixed point: x=199989
-----> found a fixed point: x=199988
-----> found a fixed point: x=199987
-----> found a fixed point: x=199986
-----> found a fixed point: x=199985
-----> found a fixed point: x=199984
-----> found a fixed point: x=199983
-----> found a fixed point: x=199982
-----> found a fixed point: x=199981
-----> found a fixed point: x=1
It takes 62 milli
It takes 1139 iterations.

This is a typical fixed point problem, start from some arbitrary number and keep acting f on it. Several outputs of the series are:
1. It goes to infinity.
2. It goes to some fixed point.
3. It keeps bouncing back and forth(going nowhere).

Two types of fixed points are attractors and opposite attractors. Attractors are attracting nearby points, i.e., if we start from some point "close" enough to the attractor, the series will converge to the attractor. Opposite attractor is just the opposite, the series will go away from the opposite attractor.
分享到:
评论

相关推荐

    Google 招聘的21道题目

    根据给定的信息,我们可以推断出这是一篇关于Google招聘测试的文章,具体是关于Google实验室能力倾向测试(GLAT)的介绍与解析。虽然提供的内容片段并不完整,但基于现有信息,我们可以对提及的每一道题目进行详细...

    IT行业笔试汇总(C,设计模式)

    - **背景介绍**:2004年10月,Google在几本专业杂志上发布了名为“Google实验室能力倾向测试”(GLAT)的一份特殊招聘试题。这份试题在全球范围内引起了广泛的关注,不仅因为它出自全球领先的科技公司之一Google,还...

    依巴谷输入星表集第2版数据文档.pdf

    依巴谷输入星表集(第2版)是欧洲空间局(ESA)为依巴谷天体测量卫星工作使用的输入星表,该卫星在轨运行四年,从1989年11月到1993年3月间返回了高质量的科学数据。该数据集于1993年对外释放。 在国家科技基础平台...

    opengl库文件glut头文件和库

    下载后解开压缩包,里面有五个文件:glut.h,glut.lib,glut32.lib,glut.dll,glut32.dll。 然后把.h文件放到VC的include路径下的GL文件夹下,VC++6.0版本对应的文件夹是安装路径下VC98\Include\GL。...

    idl代码与Matlab-NCAR-GLOW:CMake,Meson,Matlab,GLOW的Python增补。NCAR只想发布基本代码

    idl代码与Matlab 辉光 Global airglOW模型,可以从Fortran 2003编译器中独立且轻松地进行访问。 可选提供脚本语言,包括: Python≥3.6 Matlab的 GNU八度≥4.2 IDL ...glat , glon , Q , Echar , Nb

    利用日期,经纬度求日落时间的C语言程序文件.pdf

    2. **纬度和经度处理**:`input_glat`和`input_glong`函数分别用于获取用户输入的纬度和经度,以度分秒的形式。纬度范围限制在0°至60°之间,经度可以是负值,表示西经。 3. **时间计算**:`t_century`函数将从...

    pcloud2grid:此函数将点云转换为 MATLAB 样式的网格。-matlab开发

    接收 xyz 格式的点云,然后将其转换为具有纬度向量、经度向量、glat/glon 网格(使用 meshgrid)和 az(高度)矩阵的 MATLAB 样式的网格。 然后将这些保存到指定的输出 .mat 文件中。

    MilkMan:银河计划数据缩减工具

    它的构建允许任何人检查志愿者为数据库中或任何 GLON、GLAT 位置的任何主题(图像)创建的分类。 对于每个图像,您都可以看到原始志愿者绘图、聚类结果和该地区 SIMBAD 上的对象(这一点还没有完全完成)。 例如...

Global site tag (gtag.js) - Google Analytics