Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 376563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:36:40+00:00 2026-05-12T14:36:40+00:00

My day job is mainly coding in vb.net, so I am very familiar with

  • 0

My day job is mainly coding in vb.net, so I am very familiar with it. While doing my first few dozen project euler problems, I used vb.net just to get the hang of the problem styles. Now I’d like to use project euler to help me learn a new language and have been running a couple in python. However. I’ve hit a snag.

The following code will print the largest prime factor of a given number:

Protected Function isPrime(ByVal n As Long) As Boolean
    If n = 2 Then
        Return True
    End If
    If n Mod 2 = 0 Then
        Return False
    End If

    Dim maxFactor As Long = Math.Sqrt(n)
    Dim i As Integer = 3
    While i <= maxFactor
        If n Mod i = 0 Then
            Return False
        End If
        i = i + 2
    End While
    Return True
End Function

Protected Sub LargestPrimeFactor(ByVal n As Long)
    Dim factor As Long = Math.Sqrt(n) ''#largest factor of n will be sqrt(n)
    While factor > 2
        If (n Mod factor) = 0 Then
            If isPrime(factor) Then
                Me.lblAnswer.Text = factor
                factor = 0
            End If
        End If
        factor = factor - 1
    End While
End Sub

This vb.net code runs perfectly. The equivalent in python, I believe to be:

from math import sqrt

    def IsPrime(n):
        if n==2: return true
        if not n % 2: return false

        maxFactor = sqrt(n)
        i = 3
        while i <= maxFactor:
            if not n % i: return false
            i += 2
        return true

    n = 600851475143
    factor = sqrt(n)
    while factor > 2:
        if not n % factor:
            if IsPrime(factor):
                print factor
            factor = 0
        factor -= 1

However, the factor never ends up printing. Am I missing a nuance of python? Where might I have gone wrong? Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:36:41+00:00Added an answer on May 12, 2026 at 2:36 pm

    Previous solutions generate wrong answer.

    1. VB.net code operates on integers, and your Python code operates on floats, and this apparently fails somewhere.
    2. As mentioned before, keyword capitalization (True/False).
    3. You can use foo % bar == 0 with no problem.
    4. You missed one level of indentation in “factor = 0” line.

    This code produces the correct answer, 6857:

    from math import sqrt
    
    def IsPrime(n):
        if n==2: return True
        if n % 2 == 0: return False
    
        maxFactor = long(sqrt(n))
        i = 3
        while i <= maxFactor:
            if n % i == 0: return False
            i += 2
        return True
    
    n = 600851475143
    factor = long(sqrt(n))
    while factor > 2:
        if n % factor == 0:
            if IsPrime(factor):
                print factor
                factor = 0
        factor -= 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My day job involves coding with Perl. At home I play around with Python
My day job is using the compact framework but as a home/side project I'm
Today is my first day at a new job (a Front End Lead on
The ASP.NET application that I am currently responsible for at my day job has
I work with Visual Studio in my day to day job and I love
We have a batch job that runs every day and copies a file to
I have a job stored on a database, scheduled to run every day. But
Simple question, I think. I have org.springframework.scheduling.quartz.CronTriggerBean triggering one job once a day. Because
In the day job, I work on a VB6 (I know, but don't mock
Lately as part of my day job I've been learning IBM Rhapsody and using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.