[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 60 KB, 768x768, 768px-Python.svg.png [View same] [iqdb] [saucenao] [google]
10141857 No.10141857 [Reply] [Original]

a = 3
b = 2
x=1

while x<=b:
if a % x == 0 and b % x == 0:
print(x)
x = x+1
continue


print('This is HCF',x)

>> No.10141875

>>10141857
What do you want it to do?

>> No.10141902

>>10141857
x=x+1 depends on the if-statement. Set x=0 and move the "x=x+1" before if-statement.

>> No.10141915

>>10141857
>x = x+1
lrn2python
x+=1

>> No.10142038

>>10141902
Thanks Man

>> No.10142057
File: 4 KB, 420x182, 2345.png [View same] [iqdb] [saucenao] [google]
10142057

You mean this?

>> No.10142070

>>10142057
Just HCF between a and b. where b is the smaller no

>> No.10142072

>>10141857
>python

>>>/g/tfo

>exponential algorithm for gcd

>>>/g/tfo you retarded cs major

>> No.10142207

>>10141857
>a = 3
>b = 2
>x=1
>while x<=b:
>if a % x == 0 and b % x == 0:
>print(x)
>x = x+1
>continue
>print('This is HCF',x)
not science or math

>> No.10142225
File: 4 KB, 497x174, 2345.png [View same] [iqdb] [saucenao] [google]
10142225

>>10142070
Fuck, deleting my previous post because I was still young and retarded when I posted it.

While loop isn't really the best way to do this.

>> No.10142227

>>10142072
actually universities now adays forces you to learn either python or java

>> No.10142234

>>10142227
>java
Why? If you are studying in a western univeristy you're not likely to end up working in some pajeet code farm, are you?

>> No.10142252
File: 2.97 MB, 2200x3276, CS school.png [View same] [iqdb] [saucenao] [google]
10142252

>>10142227

>> No.10142305

>>10142234
>>10142252
I mean absolutely no one picks java, and in the later years you need to learn C++
Python is pretty useful when you want to do quick simulations or plots, but is definitely inferior to C++

>> No.10142316

>>10142305
Python is your cute fuckfriend, Java is a fat, smelly and ugly prostitute.

>> No.10142365

>>10141857
that's still a loop

>> No.10142506

>>10142316
C is a trophy wife

>> No.10142510

>>10142316
>>10142506
What's the based, wholesome girl you hope to marry one day? Julia???

>> No.10142515

>>10142510
>What's the based, wholesome girl you hope to marry one day?
Haskell
But then you understand that she is useless retard and dump her for whores/fuckbuddies/trophy wife

>> No.10142614

>>10142305
>is definitely inferior to C++
But python isn't even pretending to be the same thing that C/C++ is. The whole point of python is that you can quickly write up something that makes the computer do something you want it to do. It's designed around readability and ease of use, not bleeding edge performance (even though it has plenty of C wrapper libraries that do number crunching reasonably quickly). I find myself scouring the internet for GUI applications that do some thing I need done and then deleting them afterward much less now that I can simply do python.

What's more, python feels a lot like talking to the computer in English. It might not be optimal performance but it took me like 3 weeks to pick it up - only having basic C++ experience from doing some Arduino scripts before.

>> No.10142631
File: 1.15 MB, 1200x1800, 5413470+_b00ee27c1025158a0b16355cc8accbe6.png [View same] [iqdb] [saucenao] [google]
10142631

>>10142510
C++

>> No.10142651

>>here you go C++ buddies, python is freaking unreadable
#include <iostream>
int main()
{
int a = 3;
int b = 2;
int x = 1;

while (x <= b)
{
if ((a % x == 0) && (b % x == 0))
{
std::cout << x;
x++;
}

}
std::cout << "This is HFC " << x << std::endl;

return 0;
}

>> No.10142655

>>10142651
#include <iostream>
int main()
{
int a = 3;
int b = 2;
int x = 1;

while (x <= b)
{
if ((a % x == 0) && (b % x == 0))
{
std::cout << x;
x++;
}
}
std::cout << "This is HFC " << x << std::endl;

return 0;
}

>> No.10142666

>>10142651
>{

Don't do that

>> No.10142672

>>10142631
based R-fu

>> No.10142686

#include <iostream>
#include <algorithm>

int main(){
int a = 3;
int b = 2;

auto HFC=[](auto a, auto b) constexpr {
if(a<0 || b<0) return 0;
while(b){
a%=b;
std::swap(a,b);
}
return a;
};

std::cout << "This is the HFC: " << HFC(a,b) << std::endl;

return 0;
}

>> No.10142697

>>10142666
It's superior and you know it. The readability is so much better. There's a reason it's the standard for the most popular C++ graphics engine on the market.

>> No.10142701

>>10142666
Just trolling. Brackets wars are ridiculous.

>> No.10144330

>>10141857
I think you should use a simple recursive algorithm

Here is the pseudo code

gcd(x , y)
If x == 0
return y
return gcd(y%x , x)

Now you don't have to worry about which number is bigger or smaller

Suppose we have to calculate gcd(4 , 3)

Then after first step it will be converted to gcd(3 , 4).

An interesting case to solve now is if one of x ,y or both of x and y are negative.

>> No.10144441
File: 115 KB, 966x385, greatestcommonfactor.png [View same] [iqdb] [saucenao] [google]
10144441

>>10141857
without indentation it is difficult to tell if your syntax is correct. It sounds like you need to put the x=x+1 outside of the if block. get rid of continue. x+=1 is shorter way to do x = x+1.
Here is how I would do it.

>> No.10144451

>>10144441
Talk about an overkill.

Compare this to >>10142225

>> No.10144456

>>10144451
>[x for x in range(x, b+1) if not (a%x or b%x)]
That's super confusing, I'd rather make something a little longer but easier to understand. I think it's odd to call an 8 line function overkill,

>> No.10144460

>>10141915
relax bud. we all started with print("Hello Fag!")

>> No.10144465

>>10142631
>perl is a dude
OH NO NO NO NO NO HAAAHAHAHAHA

>> No.10144478

>>10144451
The only non-cosmetic difference between the two variants is that >>10144441 uses an AND statement which means he's going to have to check both conditions every time. They are both fairly inefficient because they require that every single integer smaller than the smallest number be checked. >>10144330 has got the best algorithm in this thread because it's the only one that doesn't try to brute force the issue.

>> No.10144499

>>10144456
>not fucking with list comprehensions all day erryday

>> No.10144512

>>10141857
b % x does not equal 0 on the second iteration, therefore x never increases again.

>> No.10144513

>>10141857
>x<=b:
>b % x == 0:
Wæt?

>> No.10144514

>>10144512
Sorry, I meant a % x.

>> No.10145111

function hcf(a, b) {
a = BigInt(a);
b = BigInt(b);
let counter = a > b ? a : b;
let aRem = counter % a;
let bRem = counter % b;
for (; aRem || bRem; ++counter) {
if (++aRem === a) aRem = 0n;
if (++bRem === b) bRem = 0n;
}
return counter;
}

>> No.10145322

>>10144499
Literally just semantics.