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 3977488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:55:47+00:00 2026-05-20T04:55:47+00:00

Sorry for unclear title, but I don’t know how to state it properly (feel

  • 0

Sorry for unclear title, but I don’t know how to state it properly (feel free to edit), so I will give example:

sqrt(108) ~ 10.39… BUT I want it to be like this sqrt(108)=6*sqrt(3) so it means expanding into two numbers

So that’s my algorithm

i = floor(sqrt(number))                  //just in case, floor returns lowest integer value :)
while (i > 0)                            //in given example number 108
  if (number mod (i*i) == 0)
    first = i                            //in given example first is 6
    second = number / (i*i)              //in given example second is 3
    i = 0
  i--

Maybe you know better algorithm?

If it matters I will use PHP and of course I will use appropriate syntax

  • 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-20T04:55:47+00:00Added an answer on May 20, 2026 at 4:55 am

    There is no fast algorithm for this. It requires you to find all the square factors. This requires at least some factorizing.

    But you can speed up your approach by quite a bit. For a start, you only need to find prime factors up to the cube root of n, and then test whether n itself is a perfect square using the advice from Fastest way to determine if an integer's square root is an integer.

    Next speed up, work from the bottom factors up. Every time you find a prime factor, divide n by it repeatedly, accumulating out the squares. As you reduce the size of n, reduce your limit that you’ll go to. This lets you take advantage of the fact that most numbers will be divisible by some small numbers, which quickly reduces the size of the number you have left to factor, and lets you cut off your search sooner.

    Next performance improvement, start to become smarter about which numbers you do trial divisions by. For instance special case 2, then only test odd numbers. You’ve just doubled the speed of your algorithm again.

    But be aware that, even with all of these speedups, you’re just getting more efficient brute force. It is still brute force, and still won’t be fast. (Though it will generally be much, much faster than your current idea.)

    Here is some pseudocode to make this clear.

    integer_sqrt = 1
    remainder = 1
    
    # First we special case 2.
    while 0 == number % 4:
        integer_sqrt *= 2
        number /= 4
    
    if 0 == number / 2:
        number /= 2
        remainder *= 2
    
    # Now we run through the odd numbers up to the cube root.
    # Note that beyond the cube root there is no way to factor this into
    #    prime * prime * product_of_bigger_factors
    limit = floor(cube_root(number + 1))
    i = 3
    while i <= limit:
        if 0 == number % i:
            while 0 == number % (i*i):
                integer_sqrt *= i
                number /= i*i
            if 0 == number % (i*i):
                number /= i
                remainder *= i
            limit = floor(cube_root(number + 1))
        i += 2
    
    # And finally check whether we landed on the square of a prime.
    
    possible_sqrt = floor(sqrt(number + 1))
    if number == possible_sqrt * possible_sqrt:
        integer_sqrt *= possible_sqrt
    else:
        remainder *= number
    
    # And the answer is now integer_sqrt * sqrt(remainder)
    

    Note that the various +1s are to avoid problems with the imprecision of floating point numbers.

    Running through all of the steps of the algorithm for 2700, here is what happens:

    number = 2700
    integer_sqrt = 1
    remainder = 1
    
    enter while loop
        number is divisible by 4
            integer_sqrt *= 2 # now 2
            number /= 4 # now 675
    
        number is not divisible by 4
            exit while loop
    
    number is not divisible by 2
    
    limit = floor(cube_root(number + 1)) # now 8
    i = 3
    enter while loop
        i < =limit # 3 < 8
            enter while loop
                number is divisible by i*i # 9 divides 675
                    integer_sqrt *= 3 # now 6
                    number /= 9 # now 75
    
                number is not divisible by i*i # 9 does not divide 75
                    exit while loop
    
            i divides number # 3 divides 75
                number /= 3 # now 25
                remainder *= 3 # now 3
    
            limit = floor(cube_root(number + 1)) # now 2
    
        i += 2 # now 5
    
        i is not <= limit # 5 > 2
            exit while loop
    
    possible_sqrt = floor(sqrt(number + 1)) # 5
    
    number == possible_sqrt * possible_sqrt # 25 = 5 * 5
        integer_sqrt *= possible_sqrt # now 30
    
    # and now answer is integer_sqrt * sqrt(remainder) ie 30 * sqrt(3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the unclear title, an example will clear things up: TABLE: Scenario_victories ID
Sorry for unclear title - I don't know how to describe that problem in
Sorry for unclear subject but i really don't understand where problem. I use a
Sorry, I know this question is easy, but I don't know how to get
Sorry for unclear title but I couldn't find anything that could suit my problem.To
Sorry if the title is a bit unclear/ambiguous but I am unsure of how
sorry for the unclear title but I couldn't come up with anything better. My
Sorry if the title is unclear, but I wasn't exactly sure how to describe
I'm sorry if question title was unclear, but with my cheap english, I cant
Im sorry for if the question is bit unclear. please feel free to clearup/generelize

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.