Rust、D、Go三者的Pk1

（注：虽然此题已在 以下两篇文章中以“数学公式”法做过比较，但对运行效率的比较意义不大。

http://blog.csdn.net/iilovetopview/article/details/43745059

http://blog.csdn.net/iilovetopview/article/details/43913027

http://blog.csdn.net/iilovetopview/article/details/43960963

/*题目：

有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。
比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么？

*/

一、用穷举法查5万以内的数，并显示出函数结果。

1、Rust:

use std::time::duration::Duration;
use std::collections::HashMap;

fn main() {
println!("(Rust Language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:");
let r = Duration::span(fnx);
println!(" time :{} seconds",r.num_seconds());
}

fn fnx()
{
let ( mut pos,mut n ,mut count,mut y)=(0,1,0,0);
let  mut  aa: HashMap<usize, usize> =  HashMap::new();

while(n <= 50000)
{

y+=1;

pos = Fx(n);
aa.insert(n,pos);
print!("{},",pos);
if(n == pos)
{
count +=1;
println!("n is: {0},  Fn(n) is:{1} ",n,pos);
if(count >1) {break;}
}
if(y ==10)
{
print!("\r\n");
y =0;
}
n+=1;
}
println!("{} stop",n);
}

fn Fx (n: usize) -> usize
{
let mut total =0;

for i in 0..n+1{
total += Count(i);
}
}

fn Count(n: usize) ->usize
{
let (mut num,mut t) = (0,n);

while(t >= 1)
{
if(t%10 ==1){num +=1;}
t /= 10;
}
return num;
}



2、D：

module Fnall5;
import std.stdio;
import std.array;
import std.conv;
import std.datetime;
void main()
{
writeln("(D language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:");
int n =1;
int count,pos,y;
int[int] aa;
StopWatch sw;
sw.start();
while(n <= 50000)
{
y++;
pos = Fn(n);
aa[n] = pos;
write(pos,",");
if(n == pos)
{
count++;
writeln("n is: ",n, " Fn(n) is: ",pos);
if(count >1) break;
}
if(y == 10)
{
y = 0;
writeln();
}
n++;
}
sw.stop();
writeln(" time :" , sw.peek().seconds," seconds");// msecs/1000.0
writeln(n," stop");
}
int Fn(int n)
{
int total;
for(int i=0;i<=n;i++)
{
total += Count(i);
}
}
int Count(int n)
{
int num;
for(int t = n;t >=1;t=t/10)
{
if(t%10 == 1) num++;
}
return num;
}

3.Go:

package main
import ("fmt"
"time"
)

func main() {
fmt.Println("(go Language)  Please wait for some minutes,will found Next Fn(n) = n ,the n is:")
n := 1
count :=0
pos := 0
aa := make(map[int]int)
y:=0
t :=time.Now()
for n =1;n<=50000;n++ {
pos = Fn(n)
aa[n] = pos
y++
fmt.Print(pos,",")
if n == pos {
count++
fmt.Println("n is :",n," Fn(n) is :",pos)
if(count >1) {break}
}
if(y == 10){
y =0
fmt.Println()
}

}
fmt.Println("time is :",time.Now().Sub(t).String())
fmt.Println(n," stop")
}

func Fn(n int) int {
var total int
for i:=0;i<=n;i++ {
total += Count(i)
}
}

func Count(n int) int{
var num int
for t := n;t >=1;t=t/10 {
if(t%10 == 1) {
num++
}
}
return num
}

----------------------------------------------------------------------------------------------

1、Rust:（此处只列出与“5万”不同的代码）

fn fnx()
{
let ( mut pos,mut n ,mut count)=(0,1,0);
let  mut  aa: HashMap<usize, usize> =  HashMap::new();

while(n <=100000)
{
pos = Fx(n);
aa.insert(n,pos);

if(n == pos)
{
count +=1;
println!("n is: {0},  Fn(n) is:{1} ",n,pos);
if(count >1) {break;}
}

n+=1;
}
println!("{} stop",n);
}

2、D：

void main()
{
writeln("(D language) Please wait for some minutes,will found Next Fn(n) = n ,the n is:");
int n =1;
int count,pos;
int[int] aa;
StopWatch sw;
sw.start();
while(n <= 100000)
{
pos = Fn(n);
aa[n] = pos;
if(n == pos)
{
count++;
writeln("n is: ",n, " Fn(n) is: ",pos);
if(count >1) break;
}
n++;
}
sw.stop();
writeln(" time :" , sw.peek().seconds," seconds");//1000.0
writeln(n," stop");
}

3、go:

func main() {
fmt.Println("(go Language)  Please wait for some minutes,will found Next Fn(n) = n ,the n is:")
n := 1
count :=0
pos := 0
aa := make(map[int]int)

t :=time.Now()
for n =1;n<=100000;n++ {
pos = Fn(n)
aa[n] = pos

if n == pos {
count++
fmt.Println("n is :",n," Fn(n) is :",pos)
if(count >1) {break}
}
}
fmt.Println("time is :",time.Now().Sub(t).String())
fmt.Println(n," stop")
}


1、Rust:

2、D：

3、Go:

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120