Golang Math Can Not Finished with My Code, But Python Is Ok

Why does my code work in IDLE but not when I save the file and double click the file

It's about the character π (unicode '\u03c0') in your code, the default encoding for python interpreter is ASCII, which means you cannot use a non-ascii character in your source code without telling the system.
You can either 1)use pi instead of π, or 2)add a line at the head of your code: #coding=utf-8 to tell the interpreter that you are using the utf-8 encoding in your source code.

Tell me what's wrong with this code GOLANG

Actually the code in question works on unix systems, but usually the problem is that calls like fmt.Scanf("%f", &x1) does not consume newlines, but quoting from package doc of fmt: Scanning:

Scan, Fscan, Sscan treat newlines in the input as spaces.

And on Windows the newline is not a single \n character but \r\n, so the subsequent fmt.Scanf() call will proceed immediately without waiting for further input from the user.

So you have to add a newline to your format string to avoid subsequent fmt.Scanf() call to proceed:

fmt.Scanf("%f\n", &x1)

But easier would be to just use fmt.Scanln() and skip the whole format string:

fmt.Scanln(&x1)

Scanln, Fscanln and Sscanln stop scanning at a newline and require that the items be followed by a newline or EOF.

The scanner functions (fmt.ScanXXX()) return you the number of successfully scanned items and an error. To tell if scanning succeeded, you have to check its return value, e.g.:

if _, err := fmt.Scanln(&x1); err != nil {
fmt.Println("Scanning failed:", err)
}

Is there any way to increase the performance of my python code without using threads/processes?

Also, for starters, you dont need to check all numbers up to "number+1". It is sufficient to test all numbers up to int(sqrt(number))+1. If you find a prime number, you divide your original number by that and repeat (recursively). At the end of this recursion, you will be left with some number which is itself prime and there will be no divisor found until sqrt(N). If that is the case, the number itself is a prime. I think with this method you will find primes quicker. E.g. for 3x7x7, you will just need 2+4+4 steps, instead of going through all the numbers. The last prime in the recursion should also be the largest prime factor, if I am not mistaken.

About code efficiency: Generally one should try to avoid for loops in python. I recommend you to look into e.g. the primefac module. I think you can get the full prime factorization of a number n by primefac.primefac(n) and then you can just pick its maximum (I am not too familiar with this module tho.)

Python math is wrong

You have reached a new level in computer science, and you are coming of age.

You therefore are now ready for the next step. I have been authorized by the BDFL himself to reveal the following Super Secret document to you. The ancients understood it and deciphered it first, and now, so will you!

The Floating Point Guide

Treat this document with care! Only share this with people you know have reached the same baffling conclusions!



Moderator's Note

This answer is not representative of the expected quality standards on Stack Overflow. However, it has unexpectedly developed a life of its own and is solely preserved for historical significance now.

Performance of Google's Go?

The Go math package is largely written in assembler for performance.

Benchmarks are often unreliable and are subject to interpretation. For example, Robert Hundt's paper Loop Recognition in C++/Java/Go/Scala looks flawed. The Go blog post on Profiling Go Programs dissects Hundt's claims.

Does Go have if x in construct similar to Python?

There is no built-in operator to do it in Go. You need to iterate over the array. You can write your own function to do it, like this:

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

Or in Go 1.18 or newer, you can use slices.Contains (from golang.org/x/exp/slices).

If you want to be able to check for membership without iterating over the whole list, you need to use a map instead of an array or slice, like this:

visitedURL := map[string]bool {
"http://www.google.com": true,
"https://paypal.com": true,
}
if visitedURL[thisSite] {
fmt.Println("Already been here.")
}


Related Topics



Leave a reply



Submit