Ask Your Question
0

Loading an integer from .txt file to SageMath for factoring

asked 2025-02-28 15:33:32 +0100

Kazzie gravatar image

I am trying to load a .txt file containing a very large number into SageMath for factoring. I am new to this and have tried every single method I can find on the web getting a different error every time. Can someone please talk me through this?

edit retag flag offensive close merge delete

Comments

Can you provide a toy example? Does your .txt file just contain something like "1023", or is there more in it than just a number?

John Palmieri gravatar imageJohn Palmieri ( 2025-02-28 19:40:45 +0100 )edit

It is a number over 13,000,000 digits long. It is the product of powers of up to the first 26 primes and I need to factor it.

Kazzie gravatar imageKazzie ( 2025-03-01 12:57:16 +0100 )edit

When I put in the second line below: f=open("C:\Users\karen\Desktop\factcipher.txt", "w", encoding="utf8") I get SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Kazzie gravatar imageKazzie ( 2025-03-01 13:02:00 +0100 )edit

The above command should be sage: f=open("C:\Users\karen\Desktop\factcipher.txt", "w", encoding="utf8")

Kazzie gravatar imageKazzie ( 2025-03-01 13:03:22 +0100 )edit

Are you sure it's a text file? The error suggest there are some non-text characters.

Max Alekseyev gravatar imageMax Alekseyev ( 2025-03-01 13:21:45 +0100 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2025-03-01 08:04:38 +0100

Emmanuel Charpentier gravatar image

updated 2025-03-01 08:06:45 +0100

Toy solution :

Create an example :

sage: # Let's generate (the string representation of) a large integer 
sage: foo=str(frac(pi).n(digits=3000))
sage: # Put that (except the initial "0.") on a file
sage: f=open("pifrac.txt", "w", encoding="utf8")
sage: f.write("%s\n"%foo[2:])
3001
sage: f.close()

Read that string :

sage: # Read that back in a string
sage: f=open("pifrac.txt", "r", encoding="utf8")
sage: bar=f.readline()
sage: f.close()

Convert it :

sage: gee=eval("Integer(%s)"%bar)

Check it :

sage: # What's that ?
sage: type(gee)
<class 'sage.rings.integer.Integer'>
sage: # Magnitude ?
sage: gee.log(10).n()
2999.15104072095

Check again :

sage: (frac(pi).n(digits=3000)-gee*1e-3000).log(10)
-infinity

Right...

HTH,

edit flag offensive delete link more
0

answered 2025-03-01 15:14:31 +0100

Max Alekseyev gravatar image

updated 2025-03-01 16:48:55 +0100

The error

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

can be seen even at Sagecell, where the file itself is missing. However, the error is not about the file content but about its name, which contains escape characters (backslashes). Either each of them must be doubled, or the whole string must be declared as raw:

f=open(r"C:\Users\karen\Desktop\factcipher.txt", "r", encoding="utf8")

Note the r in front of the opening quotation mark.

edit flag offensive delete link more

Comments

With

Sage: foo=str(frac(pi).n(digits=13048009))

Sage: f=open(r"C:\Users\karen\Desktop\factcipher.txt", "r", encoding="utf8")

I get error FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\karen\Desktop\factcipher.txt'

I’m sure the filename and path is correct as I opened the properties and copied the location from there.

Kazzie gravatar imageKazzie ( 2025-03-01 17:54:51 +0100 )edit

Then you have a discrepancy somewhere in the file path/name. E.g. it may contain a space at the end of its name or a character that is different from one of those listed (eg., e instead of é) or alike.

Max Alekseyev gravatar imageMax Alekseyev ( 2025-03-01 20:29:40 +0100 )edit

Hi Max I appreciate your help. I've checked the file path about 100 times for spaces etc and I've copied and pasted it from the properties. I can't see how it can have an error. I've heard of people having problems with permissions for sage to access folders. Could it be that? the error I posted above I copied and pasted from Sage, and there are definitely no spaces or special characters, at least that are visible. I'm getting really frustrated.

Kazzie gravatar imageKazzie ( 2025-03-01 22:28:13 +0100 )edit
1

Can you check what import os; print(os.sep) says?

Max Alekseyev gravatar imageMax Alekseyev ( 2025-03-01 22:39:05 +0100 )edit

sage: import os; print(os.sep)

/

sage:

Kazzie gravatar imageKazzie ( 2025-03-01 23:14:55 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2025-02-28 15:33:32 +0100

Seen: 660 times

Last updated: Mar 01