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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:27:50+00:00 2026-06-09T00:27:50+00:00

This code gives the smallest divisor of an integer. But the problem is I

  • 0

This code gives the smallest divisor of an integer. But the problem is I have to calculate the square root. Is there a way so that I don’t have to calculate the square root explicitly?

int d,r,n;
scanf("%d",&n);
if(n%2==0)
{
    printf("2 is ans"); 
}
else
{
    r=sqrt(n);
    d=3;
    while((n%d!=0)&&d<r)
    {
        d=d+2; 
    }
    if(n%d==0)
        printf("ans is %d",d);
    else
        printf("ans is 1");
}
  • 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-06-09T00:27:52+00:00Added an answer on June 9, 2026 at 12:27 am

    Since code-efficiency was one of the tags, tweak the answers provided a bit:

    while ((n%d) && (d<n/d)) d+=2;
    

    The compiler is more likely to reuse the result of the division operator this way.

    Looking at the compiler output for gcc -O3 on the version of the loop I propose, there is only one division operation per iteration, and the result is used for both comparisons:

    L18:
            cmpl    %esi, %ecx
            jle     L13
            movl    %ebx, %eax
            addl    $2, %esi
            cltd
            idivl   %esi
            testl   %edx, %edx
            movl    %eax, %ecx
            jne     L18
            .p2align 4,,15
    L13:
    

    While, the while ((n%d) && d*d < n) d+=2; version gives:

    L8:
            movl    %ecx, %eax
            imull   %ecx, %eax
            cmpl    %ebx, %eax
            jge     L3
            movl    %ebx, %eax
            addl    $2, %ecx
            cltd
            idivl   %ecx
            testl   %edx, %edx
            jne     L8
            .p2align 4,,15
    L3:
    

    And it is clear it is doing both the multiplication and the division each iteration.

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

Sidebar

Related Questions

this code gives output but it has one problem that is when user write
I'm trying this code to access a property but it gives me an error:
In this code, for vector size, n >=32767, it gives segmentation fault, but upto
Why this code gives me wrong ? In Quick sort , I have picked
I have this problem: A positive integer is called a palindrome if its representation
I have post the similar question before,but this time the problem is different,I got
I have a code like this c.execute('CREATE TABLE IF NOT EXISTS base (ID INTEGER
This code gives me a seg fault, but when I change the x-- to
This code gives me this error:Cannot implicitly convert type ArrayList[] to ArrayList[][] at this
this code gives an instant seg fault,I tried placing printf's all over, I even

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.