Can someone provide me a simplest solution for this?
2 次查看(过去 30 天)
显示 更早的评论
回答(1 个)
John D'Errico
2019-4-25
编辑:John D'Errico
2019-4-25
Easy peasy? Pretty fast too.
tic
n = 1:50000;
M = 10.^(floor(log10(n))+1);
nsteady3 = sum(n == mod(n.^3,M));
toc
Elapsed time is 0.010093 seconds.
nsteady3
nsteady3 =
40
So it looks like 40 of them that do not exceed 50000. Of course, 1 is a steady-p number for all values of p. The same applies to 5, and 25.
If n was a bit larger, things get nasty, since 50000^3 is getting close to 2^53, thus the limits of double precision arithmetic to represent integers exactly. And of course, if p were large, that would restrict things too. I'll leave it to you to figure out how to solve this for a significantly larger problem, perhaps to count the number of solutions for p=99, and n<=1e15. (Actually, this is quite easy as long as the limit on n is not too large. So n<=5e7 is quite easy even for p that large, and it should be blazingly fast.)
I got 78 such steady-99 numbers that do not exceed 5e7. It took slightly longer, but not too bad.
Elapsed time is 5.547148 seconds.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!