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

  • SEARCH
  • Home
  • 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 6685603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:04:34+00:00 2026-05-26T05:04:34+00:00

I have a function which defines a number, than determines what the last digit

  • 0

I have a function which defines a number, than determines what the last digit is, and than does a formula based on the last number, UNTIL the number reaches ‘1’

YES this is project Euler Problem 14

after the function runs, the original number is to be decreased by 1
and then the function is to be run again.

i already realise how painfully bad this method is but once i know exactly how to get the correct answer, i will make the code alot sexier and quicker to find the answer.

my code:

def get_count():
    number = 1000000
    value = number
    count = 0
    while value != 1:
        if value % 10 == 0:
            count += 1
            value *= 0.5
        elif value % 10 == 2:
            count += 1
            value *= 0.5
        elif value % 10 == 4:
            count += 1
            value *= 0.5
        elif value % 10 == 6:
            count += 1
            value *= 0.5
        elif value % 10 == 8:
            count += 1
            value *= 0.5
        elif value % 10 == 1:
            value = 3*value + 1
            count += 1
        elif value % 10 == 3:
            value = 3*value + 1
            count += 1
        elif value % 10 == 5:
            value = 3*value + 1
            count += 1
        elif value % 10 == 7:
            value = 3*value + 1
            count += 1
        elif value % 10 == 9:
            value = 3*value + 1
            count += 1
    print(">>", count, "<<")
    number -= 1
get_count()
get_count()

But when i run this it gives me the same answer twice, instead of minusing 1 it stays the same.
am i missing something here?
I am working back from 1Million as i figured the higher “stepcount” would be a higher number, but i will eventually be proven correct or wrong.
James
EDIT:
I have solved my issue, the code for it is:

number = 1000000
def get_count():
    value = number
    count = 0
    while value != 1:
        if value % 10 == 0:
            count += 1
            value *= 0.5
        elif value % 10 == 2:
            count += 1
            value *= 0.5
        elif value % 10 == 4:
            count += 1
            value *= 0.5
        elif value % 10 == 6:
            count += 1
            value *= 0.5
        elif value % 10 == 8:
            count += 1
            value *= 0.5
        elif value % 10 == 1:
            value = 3*value + 1
            count += 1
        elif value % 10 == 3:
            value = 3*value + 1
            count += 1
        elif value % 10 == 5:
            value = 3*value + 1
            count += 1
        elif value % 10 == 7:
            value = 3*value + 1
            count += 1
        elif value % 10 == 9:
            value = 3*value + 1
            count += 1
    print(">>", number, ":", count, "<<")
num = 1000000
while num > 1:
    num -= 1
    get_count()
    number -= 1

Although this is seriously, seriously bad, and super slow to run, it does what i neeeded it to, now i shall be off, to find a way to make it faster, and find a way to give me the direct answer.

  • 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-26T05:04:35+00:00Added an answer on May 26, 2026 at 5:04 am

    Local variables don’t exist outside of their enclosing function. Every time you call this function you’re getting a whole new variable number, value and count and your assignments are all run again.

    If you want variables that outlive your function two options are to make them globals or to make the function a method of a class, and have the variables be instance variables of that class.

    Globals approach:

    number = 1000000
    value = number
    count = 0
    
    def get_count():
        global number, value, count
        while value != 1:
            if value % 10 == 0:
        # etc.
    

    Class approach:

    class Foo(object):
        def __init__(self):
            self.number = 1000000
            self.value = number
            self.count = 0
    
        def get_count():
            while self.value != 1:
                if self.value % 10 == 0:
            # etc.
    

    Note that in the class approach you need to use self. to access the instance variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some templated function which has different number of arguments due to the
I have defined a function in which i want to open a page on
I have a C program which has a function call that is defined in
I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input

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.