Ask Your Question
0

Export Answer To Text File

asked 2017-05-31 02:18:16 +0100

happys5 gravatar image

Hello,

I have read the available suggestions and they did not work.

I would like to output the number generated by 2^1000 to a text file so that I can import to my C++ program.

Please help me along.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2017-05-31 07:26:18 +0100

mforets gravatar image

from the command line:

$ sage -c 'print(2^1000)' >> 2_power_1000.txt
edit flag offensive delete link more
1

answered 2017-05-31 07:22:07 +0100

Sébastien gravatar image

updated 2017-05-31 22:37:54 +0100

:

sage: s = str(2^1000)
sage: f = open('filename.txt', 'w')
sage: f.write(s)
sage: f.close()

or using a with-statement which closes the file for you:

sage: with open('filename.txt', 'w') as f:
....:     f.write(str(2^1000))
edit flag offensive delete link more

Comments

I am still making a mistake. I have tried single backslash and double backslash with no luck. I have also tried capital "C" and lower case "c". My code mirrors yours other than that.

 
sage: s = str(2^1000)
sage: f = open('C:\Users\miram\OneDrive\Documents\Sage\test.txt', 'w')
sage: f.write(s)
sage: f.close()

Any advice?

happys5 gravatar imagehappys5 ( 2017-06-01 04:06:25 +0100 )edit

There must be something wrong with the file path; try something simpler, like just f=open('test.txt', 'w') to see if that works. I think you can use the command pwd from the sage prompt to show the current working directory as a path.

niles gravatar imageniles ( 2017-06-02 22:09:16 +0100 )edit
1

answered 2017-06-02 21:52:31 +0100

dom gravatar image

Make a dom.sage file containing :

s = str(2^1000)
f = open('filename.txt', 'w')
f.write(s)
f.close()

Then run using sage :

sage dom.sage

and you get a file 'filename.txt' which contains :

0715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

on a Linux operating system (with sage locally installed).

For a Windows operating system (I guess from "C: syntax") I don't know.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-05-31 02:18:16 +0100

Seen: 1,778 times

Last updated: May 31 '17