Ask Your Question
0

Loading an integer from .txt file to SageMath for factoring

asked 0 years ago

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?

Preview: (hide)

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 ( 0 years ago )

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 ( 0 years ago )

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 ( 0 years ago )

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

Kazzie gravatar imageKazzie ( 0 years ago )

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

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

2 Answers

Sort by » oldest newest most voted
0

answered 0 years ago

Emmanuel Charpentier gravatar image

updated 0 years ago

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,

Preview: (hide)
link
0

answered 0 years ago

Max Alekseyev gravatar image

updated 0 years ago

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.

Preview: (hide)
link

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 ( 0 years ago )

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 ( 0 years ago )

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 ( 0 years ago )
1

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

Max Alekseyev gravatar imageMax Alekseyev ( 0 years ago )

sage: import os; print(os.sep)

/

sage:

Kazzie gravatar imageKazzie ( 0 years ago )

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: 0 years ago

Seen: 894 times

Last updated: Mar 01