[Tutor] Python trouble

2015-12-29 Thread Nathan Clark
This is a  fibonnaci sequence generator, the colon causes a syntax error


#set variables
num_1 = 1
num_2 = 2
count = 0
terms = int(input("How many terms of the fibonnaci sequence would you
like?")


#function
while terms != count :
num_3 =num_1+num_2
print (num_3)
num_1=num_2
num_2 = num_3
count=count+1
else:
print ("finished")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python trouble

2015-12-29 Thread Alan Gauld
On 29/12/15 16:52, Nathan Clark wrote:
> This is a  fibonnaci sequence generator, the colon causes a syntax error


Please always post the full error text.
It contains much useful information.


> terms = int(input("How many terms of the fibonnaci sequence would you
> like?")

Count the parentheses...

> while terms != count :

The syntax error is marked against this line because is where
Python first finds something it can't recognise but the actual
error is further back. That's why, with syntax errors, you should
always check a line or two before the marked error point.
In particular always check for mis-matched quotes,
brackets/parens or indentation errors - these are the
most common mistakes.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python trouble

2015-12-29 Thread Joel Goldstick
On Tue, Dec 29, 2015 at 11:52 AM, Nathan Clark <26110...@gmail.com> wrote:

> This is a  fibonnaci sequence generator, the colon causes a syntax error
>
>
> #set variables
> num_1 = 1
> num_2 = 2
> count = 0
> terms = int(input("How many terms of the fibonnaci sequence would you
> like?")
>
>
> #function
> while terms != count :
> num_3 =num_1+num_2
> print (num_3)
> num_1=num_2
> num_2 = num_3
>

this can be simplified to
   num_1, num_2 = num_2, num_3


> count=count+1
> else:
> print ("finished")
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2015-12-29 Thread Steven D'Aprano
On Wed, Dec 30, 2015 at 12:00:02AM +0700, Satya Luzy wrote:
> Hello,
> I am currently working on a program to find the prime factor of n, in which
> n is a big integer. Using the Continued Fraction factorization method (I
> will provide the source code below, please don't mind the variables). 

Are you referring to this?

http://mathworld.wolfram.com/ContinuedFractionFactorizationAlgorithm.html

> It
> works perfectly when factorizing below 10 digits numbers. In my code below
> you will see on how I tried to factorize 94152743499601547, but get this
> message instead :
> -
> *Traceback (most recent call last):*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 94, in
> *
> *faktorisasi(n)*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 92, in
> faktorisasi*
> *faktorisasi_default(n,j,boundary)*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 55, in
> faktorisasi_default*
> *Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])*
> *MemoryError*
> 
> *Process finished with exit code 1*
> 
> It has undergone the boundary value of 51200 and j value of 12. It has been
> running for more than 5 hours before it suddenly stop.
> Please let me know how to fix the memory error,

Install lots more memory.

But really, the problem here is not the MemoryError, that is just the 
symptom. Five hours to factorise a 17 digit number is a sign that either 
your code has a bug, or that the algorithm is too inefficient. If it 
works for 10 digit numbers, I can guess that it probably doesn't have a 
bug) but the algorithm, or at least your implementation of it, is too 
inefficient. I can factorise that number in under 6 seconds:

py> print(pyprimes.factors.factorise(94152743499601547))
[6784787, 13877037481]

so it is certainly possible to do better.

Unfortunately factorisation is a hard problem, and algorithms which work 
in theory (as a mathematical process) may be too slow to be practical on 
even the fastest computer. For example, my factorise function can 
factorise 94152743499601547 in six seconds, but it might take weeks to 
factorise 8864739108501786973334779656429353 into [94152743499601627, 
94152743499601739].

So it may simply be that your code is perfectly correct, but is not 
efficient enough to deal with 17 digit numbers. For example, I see that 
your faktorisasi_default function keeps three lists, p, Q and A, which 
can grow very large. Large enough that you run out of memory.

I haven't studied either the factorization algorithm or your code enough 
to tell whether you can make it more efficient. I would need to 
understand the algorithm better to do that.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2015-12-29 Thread Martin A. Brown

Hello there Satya,

>I am currently working on a program to find the prime factor of n, in which
>n is a big integer. Using the Continued Fraction factorization method (I
>will provide the source code below, please don't mind the variables).

I do not know the Continued Fraction factorization method.  (I admit 
to not looking it up and simply examining your code.)

>It works perfectly when factorizing below 10 digits numbers.

Are you certain?  All 10 digit numbers?  Or just the 10 digit 
numbers you tried?  I ask because  Well, please see my second 
point below.

>In my code below you will see on how I tried to factorize 
>94152743499601547, but get this message instead :

Alan, Steven and Danny have other useful comments.  I will not 
repeat those.

You may find it a bit easier to diagnose which data structure is 
exploding in size by logging/printing the contents of the lists p, Q 
and A, which you maintain in the function faktorisasi_default().

I made a few small modifications to your code (see below) so that I 
could run it on smaller numbers.  Here are some comments:

  * I adjusted the variables accepted in faktorisasi_default so that
there is no need for using a 'global' variable.  I don't like 
using globals if it is possible to avoid.  In pure mathematical 
functions, it is usually possible to avoid using a global.  If 
you need an intermediate work product from the function (in 
addition to the result), then simply return the intermediate 
work product along with the result.

  * See below my (slight) modifications to your code.  Now, you can 
see how I'm running the program to perform some more diagnosis.
I think you have some sort of recursion termination problem in 
your functions which are implementing your factoring.  In 
short, determining I don't think that faktorisasi_default knows 
when to stop.  Here's how I drew this conclusion:

  python wuzzyluzy.py 18  # -- stops with four lines of output
  python wuzzyluzy.py 12275   # -- stops with four lines of output
  python wuzzyluzy.py 262144  # -- stops with many lines of output
  python wuzzyluzy.py 17  # -- never stops
  python wuzzyluzy.py 19  # -- never stops

Good luck in your hunt for the wily factors,

-Martin



import fractions
import math

# CFRAC
def cfract(n, boundary):
   coeff = 1
   floor_part = floor_ = math.floor(math.sqrt(n))
   denom = n - floor_part ** 2
   result = []
   result.append(int(floor_))

   if float(denom)!=0:
  for i in range(boundary-1):
 try:
floor_ = math.floor((math.sqrt(n) + floor_part) / float(denom))
 except ZeroDivisionError: # perfect square
return result

 if denom != 1:
result.append(int(floor_))
 floor_part = denom * floor_ - floor_part
 coeff = denom
 denom = n - floor_part ** 2
 common_div = fractions.gcd(coeff, denom)
 coeff /= common_div
 denom /= common_div

 if denom == 1:
result.append(int(floor_part + result[0]))
   return result

def faktorisasi_default(n, kelipatan, boundary, faktor_1, faktor_2, flag):
q = cfract(n*kelipatan, boundary)
p = [0, q[0]]
Q = [1]
A = [0, 1, q[0]]
Q.append(((n*kelipatan)-(p[1]**2))/Q[0])
# i = 2
for i in range(2,len(q)):
p.append((q[i-1]*Q[i-1])-p[i-1])
Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])
A.append((q[i-1]*A[i]+A[i-1])%(n*kelipatan))
#tabel sudah selesai

for i in range(0,len(Q)):
for j in range(i+1,len(Q)):
if flag and Q[i]==Q[j]:
print Q[i]
print Q[j]
temp = Q[i]
tempA1 = A[i+1]
tempA2 = A[j+1]

temp2 = tempA1*tempA2 % n # nilai base

if temp2 > Q[i]:
faktor_1 = int(fractions.gcd(temp2+temp, n))
faktor_2 = int(fractions.gcd(temp2-temp, n))

if faktor_1 != 1 and faktor_2 != 1:
flag = False

return faktor_1, faktor_2, flag

def faktorisasi(n):
flag = True
faktor_1, faktor_2 = 1, 1
j=1 #kelipatan
boundary=50 #iterasi CFRAC
faktor_1, faktor_2, flag = faktorisasi_default(n, j, boundary, faktor_1, 
faktor_2, flag)
while (flag):
   j+=1
   boundary*=2
   print "Nilai boundary : %d" %boundary
   print "Nilai j : %d" %j
   faktor_1, faktor_2, flag = faktorisasi_default(n, j, boundary, faktor_1, 
faktor_2, flag)
return faktor_1, faktor_2

if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
n = int(sys.argv[1])
else:
n = 94152743499601547 #untuk difaktorkan
faktor_1, faktor_2 = faktorisasi(n)
print faktor_1
print faktor_2


-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription 

Re: [Tutor] Python trouble (Nathan Clark)

2015-12-29 Thread Ricardo Martínez
Hi, the problem is in the line that says:

terms = int(input("How many terms of the fibonnaci sequence would you
like?")

is missing a closing ')'
take a look:

terms = int(input("How many terms of the fibonnaci sequence would you
like?"))

Hope you help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2015-12-29 Thread Danny Yoo
> To be honest this is probably a bit beyond the scope of the tutor
> list which is aimed at questions about the Python language and
> standard library. However I'll make a few observations (and assume
> your logic is OK since you did test it on some smaller data first)


Just to add to Alan's comments:  a MemoryError means that your program
is keeping so many things in memory that it has exhausted this
large-but-limited resource.  Typically, when this happens, a
programmer studies their program to see whether they really do need to
keep all the held values at once.

Perhaps the approach itself is doing more than it needs to do.
Unfortunately, without a deep understanding of the approach you are
taking, we can't say anything concrete about this.  From briefly
picking through:

https://math.dartmouth.edu/~carlp/PDF/implementation.pdf

it does sound like you *have* to do several clever tricks to make this
work in practice: that paper is all about techniques for making CFRAC
work on real computers.  This does not look trivial.

I don't think this is appropriate for Python-tutor, unfortunately.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2015-12-29 Thread Alan Gauld
On 29/12/15 17:00, Satya Luzy wrote:
> Hello,
> I am currently working on a program to find the prime factor of n, in which
> n is a big integer. Using the Continued Fraction factorization method 

To be honest this is probably a bit beyond the scope of the tutor
list which is aimed at questions about the Python language and
standard library. However I'll make a few observations (and assume
your logic is OK since you did test it on some smaller data first)


> -
> *Traceback (most recent call last):*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 94, in
> *
> *faktorisasi(n)*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 92, in
> faktorisasi*
> *faktorisasi_default(n,j,boundary)*
> *  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 55, in
> faktorisasi_default*
> *Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])*
> *MemoryError*

There's not much of a clue in the error message but there are several
things we can note about your code, which if cleaned up would make
it easier to test/debug and also might reduce the memory consumption.
I don't know if any of these suggestions will help with the memory error
but clarity of code can only help think about the problem.

First you communicate between functions with globals, it would be better
to return values. (For example the flag that only exists to
be set in the faktorisasi_default code so that it influences the
faktorisasi while loop.)

Second you have a lot of loops that all appear to be looping over
the same basic data set. Is it possible to combine the processing
of those loops in some way? Alternatively could you
refactor the code to break the loops into separate functions
so that the higher level algorithm is clearer?

Third you have a lot of intermediate variables that seem to add
little value but clutter the code. (see below)


> import fractions
> import math
> 
> n = 94152743499601547 #untuk difaktorkan
> flag = True
> faktor_1 = 1#deklarasi asal
> faktor_2 = 1
> 
> # CFRAC
> def cfract(n,boundary):
>coeff = 1
>floor_part = floor_ = math.floor(math.sqrt(n))
>denom = n - floor_part ** 2
>result = []
>result.append(int(floor_))
> 
>if float(denom)!=0:
>   for i in range(boundary-1):
>  try:
> floor_ = math.floor((math.sqrt(n) + floor_part) / float(denom))
>  except ZeroDivisionError: # perfect square
> return result
> 
>  if denom != 1:
> result.append(int(floor_))
>  floor_part = denom * floor_ - floor_part
>  coeff = denom
>  denom = n - floor_part ** 2
>  common_div = fractions.gcd(coeff, denom)
>  coeff /= common_div
>  denom /= common_div
> 
>  if denom == 1:
> result.append(int(floor_part + result[0]))
>return result
> 
> def faktorisasi_default(n,kelipatan,boundary):
> global faktor_1,faktor_2,flag
> 
> q = cfract(n*kelipatan,boundary)
> p = []
> Q = []
> A = []
> p.append(0)
> p.append(q[0])
> Q.append(1)
> A.append(0)
> A.append(1)
> A.append(q[0])
> Q.append(((n*kelipatan)-(p[1]**2))/Q[0])

You could combine these lines to include the initial values
directly as:

p = [0, q[0]]
Q = [1, ((n*kelipatan)-(p[1]**2))/Q[0]]
A = [0,1]

Also since it appears more than once you could assign

def f(n,k):
   return ((n*k)-(p[1]**2))/Q[0]

Where you can hopefully think of a better name than 'f'...

> 
> # i = 2
> for i in range(2,len(q)):
> p.append((q[i-1]*Q[i-1])-p[i-1])
> Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])
> A.append((q[i-1]*A[i]+A[i-1])%(n*kelipatan))
> #tabel sudah selesai
> 
> temp = 0# nilai Q yg ganda
> temp2 = 0
> tempA1 = 0  # nilai A dari Q1
> tempA2 = 0  # nilai A dari Q2

Since you don;t use these before assigning them below you
don't really need to initialise them here in the middle
of your algorithm. Just create them by assigning to them
later.

> for i in range(0,len(Q)):
> for j in range(i+1,len(Q)):

These loops look suspicious to me. They both loop over the
same data range and look like they do an awful lot of work
on the same basic data. Could they be rationalised? I may
be wrong, I haven't a clue about what the algorithm is
supposed to do. It just feels odd somehow.

> if flag and Q[i]==Q[j]:
> print Q[i]
> print Q[j]
> temp = Q[i]
> tempA1 = A[i+1]
> tempA2 = A[j+1]
> 
> temp2 = tempA1*tempA2 % n # nilai base

Do you really gain anything with the tempA variables?
Why not just use

  temp2 = A[i+1] * A[j+1] % n

> if temp2 > Q[i]:
> faktor_1 = int(fractions.gcd(temp2+temp,n))
> faktor_2 = 

Re: [Tutor] reading an input stream

2015-12-29 Thread richard kappler
Sorry it took so long to respond, just getting back from the holidays. You
all have given me much to think about. I've read all the messages through
once, now I need to go trough them again and try to apply the ideas. I'll
be posting other questions as I run into problems. BTW, Danny, best
explanation of generators I've heard, well done and thank you.

regards, Richard

On Thu, Dec 24, 2015 at 4:54 PM, Danny Yoo  wrote:

> > I think what I need to do would be analogous to (pardon if I'm using the
> > wrong terminology, at this poing in the discussion I am officially out of
> > my depth) sending the input stream to a buffer(s) until  the ETX for that
> > message comes in, shoot the buffer contents to the parser while accepting
> > the next STX + message fragment into the buffer, or something analogous.
>
> Yes, I agree.  It sounds like you have one process read the socket and
> collect chunks of bytes delimited by the STX markers.  It can then
> send those chunks to the XML parser.
>
>
> We can imagine one process that reads the socket and spits out a list
> of byte chunks:
>
> chunks = readDelimitedChunks(socket)
>
> and another process that parses those chunks and does something with them:
>
> for chunk in chunks:
> 
>
>
> It would be nice if we could organize the program like this.  But one
> problem is that chunks might not be finite!  The socket might keep on
> returning bytes.  If it keeps returning bytes, we can't possibly
> return a finite list of the chunked bytes.
>
>
> What we really want is something like:
>
> chunkStream = readDelimitedChunks(socket)
> for chunk in chunkStream:
> 
>
> where chunkStream is itself like a socket: it should be something that
> we can repeatedly read from as if it were potentially infinite.
>
>
> We can actually do this, and it isn't too bad.  There's a mechanism in
> Python called a generator that allows us to write function-like things
> that consume streams of input and produce streams of output.  Here's a
> brief introduction to them.
>
> For example, here's a generator that knows how to produce an infinite
> stream of numbers:
>
> ##
> def nums():
> n = 0
> while True:
> yield n
> n += 1
> ##
>
> What distinguishes a generator from a regular function?  The use of
> "yield".  A "yield" is like a return, but rather than completely
> escape out of the function with the return value, this generator will
> remember what it was doing  at that time.  Why?  Because it can
> *resume* itself when we try to get another value out of the generator.
>
> Let's try it out:
>
> #
>
> >>> numStream = nums()
> >>> numStream.next()
> 0
> >>> numStream.next()
> 1
> >>> numStream.next()
> 2
> >>> numStream.next()
> 3
> >>> numStream.next()
> 4
> #
>
> Every next() we call on a generator will restart it from where it left
> off, until it reaches its next "yield".  That's how we get this
> generator to return an infinite sequence of things.
>
>
> That's how we produce infinite sequences.  And we can write another
> generator that knows how to take a stream of numbers, and square each
> one.
>
> 
> def squaring(stream):
> for n in stream:
> yield n
> 
>
>
> Let's try it.
>
>
> 
>
> >>> numStream = nums()
> >>> squaredNums = squaring(numStream)
> >>> squaredNums.next()
> 0
> >>> squaredNums.next()
> 1
> >>> squaredNums.next()
> 4
> >>> squaredNums.next()
> 9
> >>> squaredNums.next()
> 16
> 
>
>
> If you have experience with other programming languages, you may have
> heard of the term "co-routine".  What we're doing with this should be
> reminiscent of coroutine-style programming.  We have one generator
> feeding input into the other, with program control bouncing back and
> forth between the generators as necessary.
>
>
> So that's a basic idea of generators.  It lets us write processes that
> can deal with and produce streams of data.  In the context of sockets,
> this is particularly helpful, because sockets can be considered a
> stream of bytes.
>
>
> Here's another toy example that's closer to the problem you're trying
> to solve.  Let's say that we're working on a program to alphabetize
> the words of a sentence.  Very useless, of course.  :P  We might pass
> it in the input:
>
> this
> is
> a
> test
> of
> the
> emergency
> broadcast
> system
>
> and expect to get back the following sentence:
>
>  hist
>  is
>  a
>  estt
>  fo
>  eht
>  ceeegmnry
>  aabcdorst
>  emssty
>
> We can imagine one process doing chunking, going from a sequence of
> characters to a sequence of words:
>
> ###
> def extract_words(seq):
> """Yield the words in a sequence of characters."""
> buffer = []
> for ch in seq:
> 

[Tutor] MemoryError

2015-12-29 Thread Satya Luzy
Hello,
I am currently working on a program to find the prime factor of n, in which
n is a big integer. Using the Continued Fraction factorization method (I
will provide the source code below, please don't mind the variables). It
works perfectly when factorizing below 10 digits numbers. In my code below
you will see on how I tried to factorize 94152743499601547, but get this
message instead :
-
*Traceback (most recent call last):*
*  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 94, in
*
*faktorisasi(n)*
*  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 92, in
faktorisasi*
*faktorisasi_default(n,j,boundary)*
*  File "C:/Users/Satya/PycharmProjects/untitled2/fact.py", line 55, in
faktorisasi_default*
*Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])*
*MemoryError*

*Process finished with exit code 1*

It has undergone the boundary value of 51200 and j value of 12. It has been
running for more than 5 hours before it suddenly stop.
Please let me know how to fix the memory error,
Thanks before.
Sincerely.

[CODE]

import fractions
import math

n = 94152743499601547 #untuk difaktorkan
flag = True
faktor_1 = 1#deklarasi asal
faktor_2 = 1

# CFRAC
def cfract(n,boundary):
   coeff = 1
   floor_part = floor_ = math.floor(math.sqrt(n))
   denom = n - floor_part ** 2
   result = []
   result.append(int(floor_))

   if float(denom)!=0:
  for i in range(boundary-1):
 try:
floor_ = math.floor((math.sqrt(n) + floor_part) / float(denom))
 except ZeroDivisionError: # perfect square
return result

 if denom != 1:
result.append(int(floor_))
 floor_part = denom * floor_ - floor_part
 coeff = denom
 denom = n - floor_part ** 2
 common_div = fractions.gcd(coeff, denom)
 coeff /= common_div
 denom /= common_div

 if denom == 1:
result.append(int(floor_part + result[0]))
   return result

def faktorisasi_default(n,kelipatan,boundary):
global faktor_1,faktor_2,flag

q = cfract(n*kelipatan,boundary)
p = []
Q = []
A = []
p.append(0)
p.append(q[0])
Q.append(1)
A.append(0)
A.append(1)
A.append(q[0])
Q.append(((n*kelipatan)-(p[1]**2))/Q[0])

# i = 2
for i in range(2,len(q)):
p.append((q[i-1]*Q[i-1])-p[i-1])
Q.append(((n*kelipatan)-(p[i]**2))/Q[i-1])
A.append((q[i-1]*A[i]+A[i-1])%(n*kelipatan))
#tabel sudah selesai

temp = 0# nilai Q yg ganda
temp2 = 0
tempA1 = 0  # nilai A dari Q1
tempA2 = 0  # nilai A dari Q2

for i in range(0,len(Q)):
for j in range(i+1,len(Q)):
if flag and Q[i]==Q[j]:
print Q[i]
print Q[j]
temp = Q[i]
tempA1 = A[i+1]
tempA2 = A[j+1]

temp2 = tempA1*tempA2 % n # nilai base

if temp2 > Q[i]:
faktor_1 = int(fractions.gcd(temp2+temp,n))
faktor_2 = int(fractions.gcd(temp2-temp,n))

if faktor_1 != 1 and faktor_2 != 1:
flag = False

def faktorisasi(n):
global flag
j=1 #kelipatan
boundary=50 #iterasi CFRAC
faktorisasi_default(n,j,boundary)
while (flag):
   j+=1
   boundary*=2
   print "Nilai boundary : %d" %boundary
   print "Nilai j : %d" %j
   faktorisasi_default(n,j,boundary)

faktorisasi(n)
print faktor_1
print faktor_2
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MemoryError

2015-12-29 Thread Steven D'Aprano
On Wed, Dec 30, 2015 at 12:00:02AM +0700, Satya Luzy wrote:
> Hello,
> I am currently working on a program to find the prime factor of n, in which
> n is a big integer. Using the Continued Fraction factorization method (I
> will provide the source code below, please don't mind the variables).
[...]

I have had a bit more time available to look at this, and I don't think 
your code is correct. I changed the value of n from 94152743499601547 to 
18. Factorising 18 should give [2, 3, 3], but your code prints:

1
1
18
2


before ending. I tried it again with n = 459, which should factorise 
to [3, 3, 3, 17], but your code prints:

1
1
1
1
1
1
1
1
1
1
18
18
459
9

Then I added an extra line to the faktorisasi_default function, at the 
very end:

print 'q =', q, 'p =', p, 'Q =', Q, 'A =', A

and ran it again with n = 459 and got these results:

q = [21, 2, 2, 1, 4, 21, 4, 1, 2, 2, 42, 
 2, 2, 1, 4, 21, 4, 1, 2, 2, 42, 
 2, 2, 1, 4, 21, 4, 1, 2, 2, 42, 
 2, 2, 1, 4, 21, 4, 1, 2, 2, 42, 
 2, 2, 1, 4, 21, 4, 1, 2, 2, 42] 
p = [0, 21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21, 
21, 15, 11, 15, 21] 
Q = [1, 18, 13, 26, 9, 2, 9, 26, 13, 18, 1, 
18, 13, 26, 9, 2, 9, 26, 13, 18, 1, 
18, 13, 26, 9, 2, 9, 26, 13, 18, 1, 
18, 13, 26, 9, 2, 9, 26, 13, 18, 1, 
18, 13, 26, 9, 2, 9, 26, 13, 18, 1] 
A = [0, 1, 21, 43, 107, 150, 248, 309, 107, 416, 21, 458, 
438, 416, 352, 309, 211, 150, 352, 43, 438, 
1, 21, 43, 107, 150, 248, 309, 107, 416, 21, 458, 
438, 416, 352, 309, 211, 150, 352, 43, 438, 
1, 21, 43, 107, 150, 248, 309, 107, 416, 21, 458]

(reformatted to make them easier to read). So you can see the problem: 
to factorise a 3 digit number, you have recorded over 200 numbers. 
And the numbers have repeating patterns, as you can see above.

I don't know if this is expected by the algorithm, or if you have made a 
programming error, but this looks very suspicious to me. At the very 
least, if you know that these repeating patterns are expected, there is 
no need to keep growing the lists and making them bigger and bigger, you 
can just build the pattern once.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] OT: How best to use Git for multi-directory projects?

2015-12-29 Thread boB Stepp
I have two situations that I wish to use Git for:  1) Multi-directory
Python projects and 2) learning Python from various books which
inevitably have suggested exercises to program solutions for.

For (1) I might have multiple projects going simultaneously.  Say
something like:

project1/
source/
tests/
data/
etc

project2/
etc

projectn/
etc

My thoughts are that each project should have its own .git file.  If
this is correct, then I further think that there should be a single
.git file to track everything in each project folder at its top level
and use git add . at the project folder level to add anything that has
been added anywhere in any subfolder of the project folder.  Am I
thinking correctly in this?

For (2) I first want to state publicly:

New Year's Resolution:

I WILL study--in parallel--the following three books from cover to
cover, doing ALL non-trivial exercises:
"Think Python, 2nd Edition" by Allen Downey
"Introduction to Computation and Programming Using Python, Revised
and Expanded Edition" by John V. Guttag
"Python Crash Course" by Eric Matthes

I hope that by saying this openly I will actually DO what I mean to
do!  I am tired of "Easter-egging" Python, able to use lots of
different features, but with no thorough understanding/mastery of
anything.  The first two books are to get down basic intro to CSc
topics which have probably evolved since the mid- to late-seventies
when I took my basic CSc courses, while the third hopefully will fill
in Python language features and syntax that the first two books skip.

But to the questions.  This seems essentially parallel to (1) in
regards to using Git.  I want to track all of the programming work I
do for these 3 books.  I anticipate a directory structure similar to:

StudyBooks/
Book1/
Ch1/
example1.py
example2.py
exercise1.py
etc
Ch2/
etc

Book2/
etc.

Since I will be studying these 3 books in parallel, would it make
sense to track everything with a single .git file at the level of
StudyBooks directory?

Hoping to more effectively use Git in the new year!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: How best to use Git for multi-directory projects?

2015-12-29 Thread Martin A. Brown

Hello there boB,

Hey, wait a second!  What time zone are you in?  You can't have 2016 
resolutions already.  Not even the New Zealanders are there yet!

>I have two situations that I wish to use Git for:  1) Multi-directory
>Python projects and 2) learning Python from various books which
>inevitably have suggested exercises to program solutions for.
>
>For (1) I might have multiple projects going simultaneously.  Say
>something like:

[snip]

>My thoughts are that each project should have its own .git file.

N.B. this is not a Python question, but ... do you mean .git 
directory?

>If this is correct, then I further think that there should be a 
>single .git file to track everything in each project folder at its 
>top level and use git add . at the project folder level to add 
>anything that has been added anywhere in any subfolder of the 
>project folder.  Am I thinking correctly in this?

>For (2) I first want to state publicly:
>
>New Year's Resolution:
>
>I WILL study--in parallel--the following three books from cover to
>cover, doing ALL non-trivial exercises:
>"Think Python, 2nd Edition" by Allen Downey
>"Introduction to Computation and Programming Using Python, Revised and 
> Expanded Edition" by John V. Guttag
>"Python Crash Course" by Eric Matthes

(Side note:  I do not know any of these three books.)

Reading cover to cover is one way to go about it.  I think that it 
helps to balance such thorough and complete study of books with the 
direct experience of writing software.

Also, I might suggest one other reference and technique--it is more 
a generic strategy for continuous learning, here applied to 
increasing your Python proficiency.

Pick one standard library module per week (or day) and read the 
whole page of documentation.  Pick a module that is relevant to some 
problem you are solving and study each function of the module.  

Alternate between the common ones (in my world:  sys, os, time, 
math, string, re, random, csv, httplib, urllib) and some others 
(hashlib, difflib, sched, bz2, itertools, multiprocessing).  Of 
course, start by tailoring your module choice to your tasks.

>But to the questions.  This seems essentially parallel to (1) in 
>regards to using Git.  I want to track all of the programming work 
>I do for these 3 books.  I anticipate a directory structure similar 
>to:
>
>StudyBooks/
>Book1/
>Ch1/
>example1.py
>example2.py
>exercise1.py
>etc
>Ch2/
>etc
>
>Book2/
>etc.
>
>Since I will be studying these 3 books in parallel, would it make
>sense to track everything with a single .git file at the level of
>StudyBooks directory?

I'm accustomed to keeping a single git repository for each distinct 
project or program I'm working on.  In the context of Python, I 
create a separate git repository for each distribution that I may 
release.  (So, if I am going to write a new "setup.py", then it is 
time for a new git repository.)

To apply this approach to your case, I would make a different git 
repository for each StudyBook.

There are different strategies depending on what you are doing with 
the software and the environment in which you are working.

Good luck and have fun with Python in 2016, since you have arrived 
there before the rest of us,

-Martin

P.S. Two questions:  should I buy some some YHOO stock and should I
   sell my Euros?

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor