Skip to content

Commit

Permalink
Use int instead of float for the example code of log time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Mar 22, 2024
1 parent fc8473c commit 7570cef
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 65 deletions.
6 changes: 3 additions & 3 deletions codes/c/chapter_computational_complexity/time_complexity.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(float n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -100,14 +100,14 @@ int logarithmic(float n) {
}

/* 对数阶(递归实现) */
int logRecur(float n) {
int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(float n) {
int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down
12 changes: 6 additions & 6 deletions codes/cpp/chapter_computational_complexity/time_complexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(float n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -96,14 +96,14 @@ int logarithmic(float n) {
}

/* 对数阶(递归实现) */
int logRecur(float n) {
int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(float n) {
int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down Expand Up @@ -153,12 +153,12 @@ int main() {
count = expRecur(n);
cout << "指数阶(递归实现)的操作数量 = " << count << endl;

count = logarithmic((float)n);
count = logarithmic(n);
cout << "对数阶(循环实现)的操作数量 = " << count << endl;
count = logRecur((float)n);
count = logRecur(n);
cout << "对数阶(递归实现)的操作数量 = " << count << endl;

count = linearLogRecur((float)n);
count = linearLogRecur((n);
cout << "线性对数阶(递归实现)的操作数量 = " << count << endl;

count = factorialRecur(n);
Expand Down
12 changes: 6 additions & 6 deletions codes/csharp/chapter_computational_complexity/time_complexity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int ExpRecur(int n) {
}

/* 对数阶(循环实现) */
int Logarithmic(float n) {
int Logarithmic(int n) {
int count = 0;
while (n > 1) {
n /= 2;
Expand All @@ -128,13 +128,13 @@ int Logarithmic(float n) {
}

/* 对数阶(递归实现) */
int LogRecur(float n) {
int LogRecur(int n) {
if (n <= 1) return 0;
return LogRecur(n / 2) + 1;
}

/* 线性对数阶 */
int LinearLogRecur(float n) {
int LinearLogRecur(int n) {
if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -181,12 +181,12 @@ public void Test() {
count = ExpRecur(n);
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);

count = Logarithmic((float)n);
count = Logarithmic(n);
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
count = LogRecur((float)n);
count = LogRecur(n);
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);

count = LinearLogRecur((float)n);
count = LinearLogRecur((n);

Check failure on line 189 in codes/csharp/chapter_computational_complexity/time_complexity.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x on ubuntu-latest

) expected

Check failure on line 189 in codes/csharp/chapter_computational_complexity/time_complexity.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x on ubuntu-latest

) expected

Check failure on line 189 in codes/csharp/chapter_computational_complexity/time_complexity.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x on macos-latest

) expected

Check failure on line 189 in codes/csharp/chapter_computational_complexity/time_complexity.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x on macos-latest

) expected
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);

count = FactorialRecur(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int expRecur(int n) {
}

/* 对数阶(循环实现) */
int logarithmic(num n) {
int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -97,13 +97,13 @@ int logarithmic(num n) {
}

/* 对数阶(递归实现) */
int logRecur(num n) {
int logRecur(int n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
int linearLogRecur(num n) {
int linearLogRecur(int n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (var i = 0; i < n; i++) {
Expand Down
8 changes: 4 additions & 4 deletions codes/go/chapter_computational_complexity/time_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func expRecur(n int) int {
}

/* 对数阶(循环实现)*/
func logarithmic(n float64) int {
func logarithmic(n int) int {
count := 0
for n > 1 {
n = n / 2
Expand All @@ -97,20 +97,20 @@ func logarithmic(n float64) int {
}

/* 对数阶(递归实现)*/
func logRecur(n float64) int {
func logRecur(n int) int {
if n <= 1 {
return 0
}
return logRecur(n/2) + 1
}

/* 线性对数阶 */
func linearLogRecur(n float64) int {
func linearLogRecur(n int) int {
if n <= 1 {
return 1
}
count := linearLogRecur(n/2) + linearLogRecur(n/2)
for i := 0.0; i < n; i++ {
for i := 0; i < n; i++ {
count++
}
return count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func TestTimeComplexity(t *testing.T) {
count = expRecur(n)
fmt.Println("指数阶(递归实现)的操作数量 =", count)

count = logarithmic(float64(n))
count = logarithmic(n)
fmt.Println("对数阶(循环实现)的操作数量 =", count)
count = logRecur(float64(n))
count = logRecur(n)
fmt.Println("对数阶(递归实现)的操作数量 =", count)

count = linearLogRecur(float64(n))
count = linearLogRecur(n)
fmt.Println("线性对数阶(递归实现)的操作数量 =", count)

count = factorialRecur(n)
Expand Down
12 changes: 6 additions & 6 deletions codes/java/chapter_computational_complexity/time_complexity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static int expRecur(int n) {
}

/* 对数阶(循环实现) */
static int logarithmic(float n) {
static int logarithmic(int n) {
int count = 0;
while (n > 1) {
n = n / 2;
Expand All @@ -97,14 +97,14 @@ static int logarithmic(float n) {
}

/* 对数阶(递归实现) */
static int logRecur(float n) {
static int logRecur(int n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}

/* 线性对数阶 */
static int linearLogRecur(float n) {
static int linearLogRecur(int n) {
if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
Expand Down Expand Up @@ -153,12 +153,12 @@ public static void main(String[] args) {
count = expRecur(n);
System.out.println("指数阶(递归实现)的操作数量 = " + count);

count = logarithmic((float) n);
count = logarithmic(n);
System.out.println("对数阶(循环实现)的操作数量 = " + count);
count = logRecur((float) n);
count = logRecur(n);
System.out.println("对数阶(递归实现)的操作数量 = " + count);

count = linearLogRecur((float) n);
count = linearLogRecur(n);
System.out.println("线性对数阶(递归实现)的操作数量 = " + count);

count = factorialRecur(n);
Expand Down
12 changes: 6 additions & 6 deletions codes/kotlin/chapter_computational_complexity/time_complexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fun expRecur(n: Int): Int {
}

/* 对数阶(循环实现) */
fun logarithmic(n: Float): Int {
fun logarithmic(n: Int): Int {
var n1 = n
var count = 0
while (n1 > 1) {
Expand All @@ -98,14 +98,14 @@ fun logarithmic(n: Float): Int {
}

/* 对数阶(递归实现) */
fun logRecur(n: Float): Int {
fun logRecur(n: Int): Int {
if (n <= 1)
return 0
return logRecur(n / 2) + 1
}

/* 线性对数阶 */
fun linearLogRecur(n: Float): Int {
fun linearLogRecur(n: Int): Int {
if (n <= 1)
return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
Expand Down Expand Up @@ -153,12 +153,12 @@ fun main() {
count = expRecur(n)
println("指数阶(递归实现)的操作数量 = $count")

count = logarithmic(n.toFloat())
count = logarithmic(n)
println("对数阶(循环实现)的操作数量 = $count")
count = logRecur(n.toFloat())
count = logRecur(n)
println("对数阶(递归实现)的操作数量 = $count")

count = linearLogRecur(n.toFloat())
count = linearLogRecur(n)
println("线性对数阶(递归实现)的操作数量 = $count")

count = factorialRecur(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def exp_recur(n: int) -> int:
return exp_recur(n - 1) + exp_recur(n - 1) + 1


def logarithmic(n: float) -> int:
def logarithmic(n: int) -> int:
"""对数阶(循环实现)"""
count = 0
while n > 1:
Expand All @@ -86,14 +86,14 @@ def logarithmic(n: float) -> int:
return count


def log_recur(n: float) -> int:
def log_recur(n: int) -> int:
"""对数阶(递归实现)"""
if n <= 1:
return 0
return log_recur(n / 2) + 1


def linear_log_recur(n: float) -> int:
def linear_log_recur(n: int) -> int:
"""线性对数阶"""
if n <= 1:
return 1
Expand Down
24 changes: 12 additions & 12 deletions codes/rust/chapter_computational_complexity/time_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,29 @@ fn exp_recur(n: i32) -> i32 {
}

/* 对数阶(循环实现) */
fn logarithmic(mut n: f32) -> i32 {
fn logarithmic(mut n: i32) -> i32 {
let mut count = 0;
while n > 1.0 {
n = n / 2.0;
while n > 1 {
n = n / 2;
count += 1;
}
count
}

/* 对数阶(递归实现) */
fn log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn log_recur(n: i32) -> i32 {
if n <= 1 {
return 0;
}
log_recur(n / 2.0) + 1
log_recur(n / 2) + 1
}

/* 线性对数阶 */
fn linear_log_recur(n: f32) -> i32 {
if n <= 1.0 {
fn linear_log_recur(n: i32) -> i32 {
if n <= 1 {
return 1;
}
let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);
let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);
for _ in 0..n as i32 {
count += 1;
}
Expand Down Expand Up @@ -157,12 +157,12 @@ fn main() {
count = exp_recur(n);
println!("指数阶(递归实现)的操作数量 = {}", count);

count = logarithmic(n as f32);
count = logarithmic(n);
println!("对数阶(循环实现)的操作数量 = {}", count);
count = log_recur(n as f32);
count = log_recur(n);
println!("对数阶(递归实现)的操作数量 = {}", count);

count = linear_log_recur(n as f32);
count = linear_log_recur(n);
println!("线性对数阶(递归实现)的操作数量 = {}", count);

count = factorial_recur(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func expRecur(n: Int) -> Int {
}

/* 对数阶(循环实现) */
func logarithmic(n: Double) -> Int {
func logarithmic(n: Int) -> Int {
var count = 0
var n = n
while n > 1 {
Expand All @@ -99,15 +99,15 @@ func logarithmic(n: Double) -> Int {
}

/* 对数阶(递归实现) */
func logRecur(n: Double) -> Int {
func logRecur(n: Int) -> Int {
if n <= 1 {
return 0
}
return logRecur(n: n / 2) + 1
}

/* 线性对数阶 */
func linearLogRecur(n: Double) -> Int {
func linearLogRecur(n: Int) -> Int {
if n <= 1 {
return 1
}
Expand Down Expand Up @@ -158,12 +158,12 @@ enum TimeComplexity {
count = expRecur(n: n)
print("指数阶(递归实现)的操作数量 = \(count)")

count = logarithmic(n: Double(n))
count = logarithmic(n: n)
print("对数阶(循环实现)的操作数量 = \(count)")
count = logRecur(n: Double(n))
count = logRecur(n: n)
print("对数阶(递归实现)的操作数量 = \(count)")

count = linearLogRecur(n: Double(n))
count = linearLogRecur(n: n)
print("线性对数阶(递归实现)的操作数量 = \(count)")

count = factorialRecur(n: n)
Expand Down
Loading

0 comments on commit 7570cef

Please sign in to comment.